Delete a Cognito User Pool on CDK Destroy in AWS CDK

avatar

Borislav Hadzhiev

Mon Apr 26 20211 min read

Deleting a Cognito User Pool on CDK Stack Deletion #

In order to delete a Cognito User Pool when our CDK stack is deleted, we need to override theremovalPolicy prop of the user pool and set it to DESTROY.

const userPool = new cognito.UserPool(this, 'user-pool', { // ...rest // ๐Ÿ‘‡ set to DESTROY removalPolicy: cdk.RemovalPolicy.DESTROY, });

If we run the cdk destroy command after we've updated the removalPolicy prop, our Cognito User Pool will get deleted along with the stack.

The Cognito User Pool would also get deleted if we delete the resource from our CDK code.

Default Behavior is to Retain Stateful Resources #

By default stateful resources (databases, tables, user pools) in AWS CDK have their removalPolicy set to RETAIN - docs.

const userPool = new cognito.UserPool(this, 'user-pool', { // ... // ๐Ÿ‘‡ default is RETAIN removalPolicy: cdk.RemovalPolicy.RETAIN, });

When the removalPolicy is set to RETAIN the Cognito User Pool will not get deleted when the stack gets deleted, instead it will remain in the account in an orphaned state.

The same is the default behavior for when we delete Dynamodb tables, S3 Buckets, and other stateful resources.

Further Reading #

Add me on LinkedIn

I'm a Web Developer with TypeScript, React.js, Node.js and AWS experience.

Let's connect on LinkedIn

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee