Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

S3 refuses to save an event notification for a Lambda function

ErrorUnable to validate the following destination configurations

Short answer

When you save a bucket notification, S3 checks the destination first. For Lambda it checks that the function's resource policy lets S3 invoke it from your bucket. If that permission is missing, or the function is in another Region, the whole save fails. Add the permission with lambda add-permission, then save again.

  • Applies to AWS CLI v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the function's resource-based policy. If there is no statement for the s3.amazonaws.com principal, S3 has no permission to invoke the function.

    aws lambda get-policy --function-name my-function --region us-east-1
  2. 2Match what you see, then jump to its fix

    1. 1get-policy shows no statement for the s3.amazonaws.com principalThe Lambda function does not allow S3 to invoke itFix
    2. 2The function exists and has the right policy, but its ARN shows a Region other than the bucket'sThe function is in a different Region than the bucketFix
    3. 3The notification file also lists TopicConfigurations or QueueConfigurationsAn SNS topic or SQS queue in the same configuration has no policy for S3Fix

What does the error look like?

  • Unable to validate the following destination configurations
  • InvalidArgument: Unable to validate the following destination configurations

Cause 1 of 3

The Lambda function does not allow S3 to invoke it

1Check

When you create the notification through the API, S3 verifies that the function permissions grant S3 the right to invoke it from your bucket. The console adds this permission for you; the CLI and SDKs do not.

aws lambda get-policy --function-name my-function --query Policy --output text

2Fix

Grant the S3 service principal permission, limited to your bucket and your account, then save the notification again.

aws lambda add-permission \
  --function-name my-function \
  --statement-id s3-invoke \
  --action lambda:InvokeFunction \
  --principal s3.amazonaws.com \
  --source-arn arn:aws:s3:::my-bucket \
  --source-account 111122223333

3Verify

The policy now lists the s3.amazonaws.com statement, and the notification saves.

aws lambda get-policy --function-name my-function --query Policy --output text
aws s3api put-bucket-notification-configuration --bucket my-bucket --notification-configuration file://notification.json

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

The function is in a different Region than the bucket

1Check

The Lambda function must be in the same Region as the S3 bucket. Compare the Region in the function ARN with the bucket's Region.

aws s3api get-bucket-location --bucket my-bucket
grep LambdaFunctionArn notification.json

2Fix

Create or copy the function in the bucket's Region and use that ARN in LambdaFunctionArn.

{
  "LambdaFunctionConfigurations": [
    {
      "LambdaFunctionArn": "arn:aws:lambda:us-east-1:111122223333:function:my-function",
      "Events": ["s3:ObjectCreated:*"]
    }
  ]
}

3Verify

Apply the file and read the configuration back.

aws s3api put-bucket-notification-configuration --bucket my-bucket --notification-configuration file://notification.json

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

An SNS topic or SQS queue in the same configuration has no policy for S3

1Check

The PUT is atomic. S3 sends a test message to each SNS and SQS destination, and if one fails the whole request fails, including the Lambda part.

grep -E "TopicArn|QueueArn" notification.json

2Fix

Attach an access policy to the topic or queue that lets s3.amazonaws.com publish, limited by aws:SourceArn to your bucket.

{
  "Effect": "Allow",
  "Principal": { "Service": "s3.amazonaws.com" },
  "Action": "SQS:SendMessage",
  "Resource": "arn:aws:sqs:us-east-1:111122223333:queue-name",
  "Condition": {
    "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:my-bucket" },
    "StringEquals": { "aws:SourceAccount": "111122223333" }
  }
}

3Verify

Apply the full notification file again.

aws s3api put-bucket-notification-configuration --bucket my-bucket --notification-configuration file://notification.json

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Define the bucket notification and the Lambda permission together in the same infrastructure template, with the permission created first.
  • Always set source-arn and source-account on the permission so no other account's bucket can invoke the function.
  • Keep the bucket and the function in one Region.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

API Gateway gives up on a slow Lambda or Node.js backend

$context.integrationErrorMessage in the access log shows the integration error

API Gateway stops waiting for the backend at its integration timeout: 29 seconds by default on REST APIs, a fixed 30 seconds on HTTP APIs.

4 causes, each with a fix

Back end

cdk synth says --app is required

--app is required either in command-line, in cdk.json or in ~/.cdk.json

The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.

3 causes, each with a fix

Back end

The AWS SDK in Node.js cannot find any credentials

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.