How do I update AWS S3 bucket using an CDK stack?

1

Hello,

I have a situation where I created a bucket in my AWS account that is logging GuardDuty logs to the bucket.

I have a case where I need to update this in an CDK stack for IaaC requirement. I will need to add event notificaiton in the future via this IaaC CDK. But at the moment, I do not need to change anything in the S3 bucket itself but I do need to have it as IaaC instead of manually.

Is there a way to create a CDK stack to deploy to an AWS account that has an existing S3 bucket with the same name, without REWRITING or DELETING AND RE-CREATING the bucket? SO that way I do not lose logs or anything else?

What would be the best steps in this situation?

1 Answer
1

Yes, it is possible to create a CDK stack that deploys to an AWS account that has an existing S3 bucket with the same name, without rewriting or deleting and re-creating the bucket.

One way to do this would be to use the aws-cdk-lib library's S3 class and pass in the name of the existing S3 bucket as a parameter. This will allow you to create a new stack that references the existing bucket without modifying or deleting it.

Another way is to use the CfnBucket class to import the existing S3 bucket into your CDK stack. This class allows you to import existing resources into your stack, and you can then add event notifications to it via the stack.

It is important to note that the permissions on the bucket should be appropriate to allow the CDK stack to access the bucket.

It would be the best approach to first export the current configuration of the bucket using the AWS CLI aws s3api get-bucket-configuration command and then import it as a CloudFormation resource in the CDK stack. This would ensure that all the current configuration is maintained and only new resources are added via the CDK stack.

profile picture
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions