AWS autodelete an s3 bucket on cdk destroy using Typescript

AWS autodelete an s3 bucket on cdk destroy using Typescript

15 minutes 48s
A step by step example of how to create an s3 bucket that autodeletes when we run cdk destroy, using Typescript

Create a stack using CDK

First we use the cdk to create a new Typescript project.


sudo npm install aws-cdk
sudo npm install @aws-cdk/aws-s3
mkdir -p mystack
cd mystack; cdk init --language typescript


A stack that creates an s3 bucket

We define a stack with an s3 bucket. In this example the bucket name is hardcoded. There are pros and cons of hardcoded vs auto-named s3 buckets.

// mystack-stack-js
import * as cdk from '@aws-cdk/core';
import * as s3 from "@aws-cdk/aws-s3";

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

    // The code that defines your stack goes here
    // most parameters are optional
    const bucket = new s3.Bucket(this, "mytestbucket", {
      versioned: false,
      // no underscores
      bucketName: 'mytestbucket',
      publicReadAccess: false,
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
    });
  }
}


For both cases, we run cdk deploy, and the bucket gets created.


# bootstrap will run once only
cd mystack;
cdk bootstrap

# or with a specific profile
#cdk --profile myprofile bootstrap

cdk deploy

# or with a specific profile
#cdk --profile myprofile deploy


So far so good. The bucket has been created in s3.


cdk destroy

# or with a specific profile
#cdk --profile myprofile destroy


The problem is, when we run cdk destroy, the s3 bucket is not removed — which may surprise us, since that seems to be the purpose of cdk destroy). This can lead to extra costs (since it is still on s3) and failed later deployments (we do not expect the folder to exist).

We need to delete the s3 buckets manually. In this example, it is only one bucket, but you can imagine how tedious it can be when several buckets are created automatically: we will need to delete them by hand, after emptying them (we cannot delete a bucket that is not empty)


The solution: AutoDeleteBucket

We can install this npm package to solve it. It is a — very convenient — drop-in replacement. You can use all the same parameters as in s3.Bucket.


# from above mystack folder
# auto-delete-bucket needs these
sudo npm install @aws-cdk/aws-lambda
sudo npm install @aws-cdk/aws-cloudformation
sudo npm install @mobileposse/auto-delete-bucket


// mystack-stack-js
import * as cdk from '@aws-cdk/core';
import * as s3 from "@aws-cdk/aws-s3";
import { AutoDeleteBucket } from '@mobileposse/auto-delete-bucket'

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

    // The code that defines your stack goes here
    // most parameters are optional
    const bucket = new AutoDeleteBucket(this, "mytestbucket", {
      versioned: false,
      // no underscores
      bucketName: 'mytestbucket',
      publicReadAccess: false,
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
    });
  }
}


Testing the solution

After replacing s3.Bucket with AutoDeleteBucket we can run deploy and destroy to confirm that the bucket has, indeed, been deleted.