aws-janitor is a small AWS CDK project that deploys a scheduled Lambda function to automatically clean up old test stacks in your AWS account.
It uses stack tags (TTL
) to determine when a stack should be deleted, ensuring that test resources don't linger and accumulate unnecessary costs.
-
Tag your CloudFormation stacks
- Add a
TTL
tag to your stack. - The value should be human-readable, e.g.,
12 hours
,3 days
, or30 minutes
.
- Add a
-
Automatic Cleanup
- The janitor Lambda will regularly scan stacks.
- If a stack's creation time + TTL is in the past, it will be automatically deleted.
-
Helpful Tooling
- See aws-cdk-tools for code to automatically add a TTL to your CDK stacks.
- Set up your virtual environment
python3 -m venv .venv
source .venv/bin/activate
- Install dependencies
pip install -r requirements.txt
- Set the deployment environment
export ENV=test # or ENV=live
- Deploy the stack
cdk deploy
cdk ls
— list all stackscdk synth
— output the synthesized CloudFormation templatecdk deploy
— deploy your stack to AWScdk diff
— compare your stack against deployed versioncdk destroy
— destroy the deployed stack
app.py
— CDK application entry pointaws_janitor/
— CDK stack definitionlambda/janitor/handler.py
— Janitor Lambda coderequirements.txt
— Python dependencies
- Lambda log retention is set to 6 months to minimize costs.
- The janitor is designed to fail safely in test environments.
- Stack tagging is automatic via a shared
BaseStack
from aws-cdk-tools.