Skip to content

bifravst/cloudformation-cleaner

AWS CloudFormation Cleaner

GitHub Actions semantic-release Renovate @commitlint/config-conventional code style: prettier ESLint: TypeScript

This is a CDK project written in TypeScript that sets up a CloudFormation stack which cleans up left-over stacks and log groups from CI runs.

Although your CI tests should clean up after themselves, there still might be stacks and log groups that get not cleaned up perfectly. This stack runs a lambda every hour, which deletes stacks and log groups that have a certain prefix and are older than 24 hours.

Install

git clone https://github.com/bifravst/cloudformation-cleaner
npm ci

Setup in your CI account

Note: you should only set this up in an account where every CloudFormation stack can be deleted, because this lambda has the permissions to delete everything.

npx cdk deploy

Configuration

You can configure the regular expression used to check against a resources names according to the following table

Lambda SSM Parameter Name
stack-cleaner /${stackName}/stackNameRegEx
log-group-cleaner /${stackName}/logGroupNameRegEx
role-cleaner /${stackName}/roleNameRegEx
buckets-cleaner /${stackName}/bucketNameRegEx
parameter-cleaner /${stackName}/parameterNameRegEx

You can configure this from the command line:

aws ssm put-parameter --name /cloudformation-cleaner/stackNameRegEx --value '^(some-pattern|another-pattern)-' --overwrite

Running from the command line

npx tsx ./cli.ts