aws-cdk: @aws-cdk/aws-s3: notification lambda function does not handle transient s3 errors well

What is the problem?

A deployment failed for our team using a CDK defined infrastructure package due to a failure in the bucket notification lambda function. After looking at the logs in our account, it appears that it was caused due to missing error handling within the bucket notification lambda function. The line that failed was this one, which is called here.

Error logs:

[ERROR] 2021-10-05T18:18:42.172Z 5948f640-82f4-44de-a218-694408fb7bfb Failed to put bucket notification configuration

put_bucket_notification_configuration(bucket, config)

botocore.exceptions.ClientError: An error occurred (OperationAborted) when calling the PutBucketNotificationConfiguration operation: A conflicting conditional operation is currently in progress against this resource. Please try again.

Reproduction Steps

Have multiple notifications attempt to update the bucket? I am not quite sure!

What did you expect to happen?

The bucket notification succeeds. To do this, adding an error handling + retry method here would be great. For transient errors like this, it seems like having more robust error handling and retrying transient errors would be very beneficial. That way all of the consumers of AWS CDK get a smoother experience here

What actually happened?

The bucket notification failed, causing the stack to get into an UPDATE_ROLLBACK_FAILED state

CDK CLI Version

1.25.0

Framework Version

n/a

Node.js Version

n/a

OS

linux

Language

Python

Language Version

3.8

Other information

List of events:

Time Resource Status Message
2021-10-05 11:18:42 UTC-0700 BucketNotifications8F2E257D UPDATE_FAILED Received response status [FAILED] from custom resource. Message returned: Error: An error occurred (OperationAborted) when calling the PutBucketNotificationConfiguration operation: A conflicting conditional operation is currently in progress against this resource. Please try again… See the details in CloudWatch Log Stream: 2021/10/05/[$LATEST]f56d3b94003f4219971717943c98f61c (RequestId: 52f908f9-40b7-4583-b6fe-314e65a991e8)
2021-10-05 11:18:46 UTC-0700 my-stack-name UPDATE_ROLLBACK_IN_PROGRESS The following resource(s) failed to update: [BucketNotifications8F2E257D, …].

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 15
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you @otaviomacedo. Is there any kind of guess on SLA for a p1 of this nature?

It would be good to be able to set expectations internally.

Hi team, would you help to have a look on this small work item which wasted us for quite a long time over the last few months?

Proposed fix by adding dependency on Bucket Policy: https://github.com/aws/aws-cdk/pull/30053

For what it’s worth, adding a dependency between the bucket policy and the notifications worked for me.

import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as s3 from 'aws-cdk-lib/aws-s3'
import * as iam from 'aws-cdk-lib/aws-iam'
import * as s3_notifications from 'aws-cdk-lib/aws-s3-notifications'
import { Construct } from 'constructs';

export class S3TestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const func = new lambda.Function(this, 'function', {
      code: new lambda.InlineCode('exports.handler = function (event, context) {context.succeed(\'hello world\');};'),
      handler: 'index.handler',
      runtime: lambda.Runtime.NODEJS_LATEST
    })

    const bucket = new s3.Bucket(this, 'bucket', {removalPolicy: cdk.RemovalPolicy.DESTROY})

    const destination = new s3_notifications.LambdaDestination(func)

    bucket.addEventNotification(s3.EventType.OBJECT_CREATED_PUT, destination)
    bucket.addEventNotification(s3.EventType.OBJECT_CREATED_POST, destination)

    bucket.addToResourcePolicy(
      new iam.PolicyStatement({
        actions: ['s3:*'],
        principals: [new iam.AccountRootPrincipal()],
        resources: [bucket.bucketArn, bucket.arnForObjects('*')]
      })
    )

    // Add dependency between policy and notifications so they aren't applied at the same time
    bucket.node.findChild('Notifications').node.addDependency(bucket.node.findChild('Policy'))
  }
}