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.
git clone https://github.com/bifravst/cloudformation-cleaner
npm ci
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
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
npx tsx ./cli.ts