← Go back to homepage
Deletion of CloudFormation Stacks in Bulk

Deletion of CloudFormation Stacks in Bulk

In the last few projects where I worked on, as part of teams, using the AWS CDK framework as a way to declare and provision Infrastructure as Code, we had a common problem: the cleanup of CloudFormation stacks. We had a lot of stacks that were created for testing, development, and production environments, and we needed to delete them. The problem was that we had to delete them one by one, and we had to wait for the deletion of one stack to start the deletion of the next one. This was a time-consuming task, and we needed to automate it.

In this post, I will show you how to automate the deletion of such CloudFormation stacks. We could use the AWS web based UI to do this, but this is a time consuming and error-prone process.

After spending some time on doing this manually, I decided to build a small Go application to automate this process. It uses the AWS SDK for Go under the hood. I've published it as an open-source project on GitHub: cf-purge.

The installation instructions are already available in the repository and are dependent on the OS you are using, so we won't be repeating them here. Instead, we will focus on how to use the tool.

I am assuming in your organization you have some sort of CI/CD that uses something like AWS CDK, SST or Terraform. I will assume your developer environments are prefixed with the developer's name (e.g. "john-dev", "jane-dev"), and your production environment is prefixed with "prod". This is a common pattern I've seen in many organizations.

Temporary environments can be created around short lived Git branches, like "feature-123", "bugfix-456", etc - where the branch name can serve as the prefix for CloudFormation stacks for that environment.

This pattern is good in practice, but you can quickly end up with a lot of stale stacks that you need to delete at some point from the AWS account:

  • When testing is completed on the given Git branch, and it's ready to be merged
  • When a developer leaves the company and their environment is no longer needed
  • When a certain environment needs to be recreated from scratch for certain technical reasons

Using the cf-purge tool, you would do it in the following way:

If the environment was a temporary environment around a Git branch called "feature-123", you would run:

cf-purge --glob "feature-123-*"

If the environment as a dev environment for an engineer and was prefixed with "jane-dev", you would run:

cf-purge --glob "jane-dev-*"

After a few minutes of waiting, the stacks should be gone. The tool will also print out the names of the stacks it deletes.

← Go back to homepage