aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.6k stars 3.9k forks source link

Update Getting Started in Dev Guide to set removalPolicy on S3 bucket so destroy will clean it up #4503

Closed rajinder-yadav closed 4 years ago

rajinder-yadav commented 5 years ago

Follow CDK Hello world here: https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

When I did the "cdk destroy" my S3 bucket was not deleted. I tried again with same result.

Environment

{
  "name": "aws-cdk",
  "version": "0.1.0",
  "bin": {
    "aws-cdk": "bin/aws-cdk.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-cdk/assert": "^1.12.0",
    "@types/jest": "^24.0.18",
    "jest": "^24.9.0",
    "ts-jest": "^24.0.2",
    "aws-cdk": "^1.12.0",
    "ts-node": "^8.1.0",
    "typescript": "~3.6.2"
  },
  "dependencies": {
    "@aws-cdk/aws-s3": "^1.12.0",
    "@aws-cdk/core": "^1.12.0",
    "source-map-support": "^0.5.9"
  }
}
shivlaks commented 5 years ago

hey @rajinder-yadav

CloudFormation does not delete S3 buckets that are non-empty.

You can only delete empty buckets. Deletion fails for buckets that have contents.

Here's the relevant documentation.

rajinder-yadav commented 5 years ago

The bucket is empty, still not being destroyed.

SomayaB commented 5 years ago

The bucket is empty, still not being destroyed.

@shivlaks

shivlaks commented 5 years ago

@rajinder-yadav oh I see that you're using the aws-s3 module. The buckets are created with a default retention policy of Retain

you can set this with the removalPolicy property:

const bucket = new s3.Bucket(this, 'MyBucket', {
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY
    });
rajinder-yadav commented 4 years ago

@shivlaks that worked for me, can you please have the "Getting Started" document updated so it won't confuse new Users.

ghost commented 4 years ago

We should probably set the removal policy in the Getting Started.

ghost commented 4 years ago

I've added this to the CDK Dev Guide repo. https://github.com/awsdocs/aws-cdk-guide/issues/159

Should get to it soon... I'll close this one.