AWS CDK with Python — 6

Pratik
4 min readAug 17, 2023

--

Destroy CDK stack

Previous Part

In the previous part, we deployed our first CDK stack with sample SQS queue and SNS topic constructs. It was created from a sample app provided by the CDK team that we used to familiarize with CDK concepts and the CDK toolkit. We won’t need these resources anymore. Then, how can you delete the resources deployed via your CDK app?

As CDK performs deployments as CloudFormation stacks, your first option to delete the resources deployed by your CDK app, is to select each stack deployed by your CDK app on the CloudFormation Console and click the ‘Delete’ button. After approval, it will delete all resources managed by your stack. However, this is the CloudFormation way.

CloudFormation Way of Deletion

How to destroy the stacks deployed by your CDK app using the CDK toolkit instead. Please don’t delete the ‘CDKToolkit’ stack by mistake, as it is the bootstrapping stack, and you will need it later to deploy other CDK apps. Otherwise, you must bootstrap your CDK environment again for this AWS region.

Now, let’s go back to our project code and use the CDK Toolkit to delete the stack we deployed. So, to delete the stacks deployed by your CDK app, you use the ‘cdk destroy’ command.

Now, as in the ‘cdk synth’ and ‘cdk deploy’ commands, you can provide stack names that will be deleted. For example, the stack name in our example is ‘my-first-cdk-app’, which was set in the ‘app.py’ file while initializing our CDK app. If you have multiple stacks, you can provide a second stack name after spaces to delete it, too. But we have a single stack in our app. So, there is no need to provide stack names.

The ‘cdk destroy’ command with no inputs will be sufficient to delete our stack. Then, let’s press Enter to execute the command.

It asks you to approve the deletion in case you executed the command by mistake.

It also provided the name of the stack that is selected for deletion. Let’s provide ‘y’ and press Enter afterward to approve. It’ll start deleting the ‘my-first-cdk-app’ stack, and it will print the stack deletion events here.

Once deletion is finished successfully, if you check the CloudFormation Console now, you won’t see our sample CDK app’s stack. The ‘my-first-cdk-app’ stack was destroyed. And all constructs under the ‘Resources’ tab switched to the ‘DELETE_COMPLETE’ state.

If you enlarge the ‘MyFirstCdkAppQueue’ construct, you can verify that all its sub-constructs, Policy and the topic subscription, and the SQS queue resource became ‘DELETE_COMPLETE’ as well. This is how to check at the construct level.

You can switch to the ‘Flat view’ to display all CloudFormation resources in the standard CloudFormation view. All CloudFormation resources were deleted. Now, the ‘Events’ tab displays the deletion events as shown on the terminal while the deletion was in progress. And the ‘Stack info’ tab shows the details of your stack. As you see, the stack switched to the ‘DELETE_COMPLETE’ state, indicating the deletion success. So, this is how to check the stack deletion on AWS CloudFormation Console.

The ‘cdk destroy’ command is beyond imitating CloudFormation’s ‘delete-stack’ command or deleting your stack from the CloudFormation Console.It may work the same in most resources, but in some constructs, it brings more capabilities to CloudFormation.

For example, if you are familiar with CloudFormation, you may know that you can’t delete a vanilla S3 bucket with objects by deleting its stack. As a solution, you can keep the S3 bucket after stack deletion by providing the ‘DeletionPolicy’ attribute in your template. But in that case, your S3 bucket will continue to exist after stack deletion, and you will continue to be charged for it.

Alternatively, you can write a Lambda function yourself, which will be triggered as a custom resource by your stack, and empty your S3 bucket before deleting it. But all of these require advanced CloudFormation and some Lambda programming knowledge.

However, if you use CDK instead, your life becomes much easier.

The L2-level S3 bucket construct already has the ‘auto_delete_objects’ property feature, which configures the custom CloudFormation resource and the Lambda function code out of the box. Hence, CDK depends on CloudFormation but simplifies its usage with powerful abstraction.

--

--