Is it possible to add NotificationConfiguration to an existing S3 buckets using CFT?

Viewed 2570

I am still a newbie with AWS services.

I would like to add a Lambda trigger on an existing S3 bucket using a CloudFormation template (CFT). Is this possible?

The following CFT is attempting to create a new S3 bucket and add an event notification on it.

    S3BUCKET_NOTIFCATION = Bucket(
        "S3Bucket",
        BucketName=s3_bucket("confidential", Ref(ENV)),
        NotificationConfiguration=NotificationConfiguration(
            LambdaConfigurations=[
                LambdaConfigurations(
                    Event="s3:ObjectCreated:*",
                    Filter=Filter(
                        S3Key=S3Key(
                            Rules=[Rules(Name="prefix", Value=Ref(inputKeyPrefix)),
                                   Rules(Name="suffix", Value=".json")]
                        )
                    ),
                    Function=Ref(cost_function)
                )
            ]
        )
    )

Is it possible to add the NotificationConfiguration to an existing bucket?

2 Answers

As noted by others this is still not allowed for various reasons.

Option 1) recommended

The ideal approach here, IMHO, would be to first import existing cloud resources (S3 in this case) to be managed by IaC/CloudFormation. Then, once the resource has been imported, you can add the NotificationConfiguration to your CloudFormation template. This approach allows you to manage existing resources that maybe were initially provisioned manually to be managed via IaC/Cfn moving forward.

Option 2) what you asked for

If you want to add NotificationConfiguration to an existing S3 bucket via CloudFormation the workaround is to use

a Lambda-backed custom resource created... The custom resource triggers a Lambda function, which triggers the PutBucketNotification API to add a notification configuration to your S3 bucket.

details here

NOTE: Please take into account the limitations with Option 2). Some of which are documented in the referenced link.

I've been unable to add NotificationConfiguration to an existing bucket as well. When you try, you'll get the error CREATE_FAILED. Reason: S3_BUCKET already exists.

This ServerFault question from 2013 details that modification of a pre-existing bucket is not allowed. It appears to still be correct.

Related