简体   繁体   中英

AWS CDK Error: bucket policy already exists on bucket

I am trying to create a lambda and a DynamoDB using CDK in java. When I try to do 'CDK Deploy' I face following error:

11:20:30 AM | CREATE_FAILED        | AWS::S3::BucketPolicy | StagingBucketPolicy
The bucket policy already exists on bucket xyz-cdk-toolkit-stagingbucket-1hpv60vcdqk7s.

CDK Version: 1.48.0

This error occurs directly from CloudFormation, and as it says, the bucket you are adding the policy to, already has an existing one . CloudFormation does not allow for replacement/modification of bucket policies that were created outside of its control.

This is a security measure preventing you by accidentally changing existing policies.

If you want to control the bucket policy with CDK (or CloudFormation) you have to recreate it in CDK, delete the original one, and create using the CDK.

As I mentioned in comment above in response to @Marcin's reply, this blog helped me solve the problem. Following are the steps I followed:

  1. Copied the cloud formation yaml snippet to a file named s3-bucket.yml provided in the blog.
  2. Used aws cli command to first create this bucket: aws cloudformation deploy --template-file resources/s3-bucket.yml --stack-name my-bucket-stack1
  3. Fired this CDK command to deploy my stack: cdk deploy --toolkit-stack-name my-bucket-stack1

These steps created my stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM