AWS CDK is driving up your S3 bill and you may not even realize it.

I had no idea AWS CDK left assets from previous stack versions around in S3. Over a few years of regular deployments it was becoming a noticeable expense on my AWS bills. Turns out cloudstructs has a tool to clean up old assets from your CDK stacks (via a CDK stack!).

Spin up a new CDK app and deploy this into your AWS account to cleanup old CDK assets automatically on a schedule.

import * as cdk from 'aws-cdk-lib';
import { Schedule } from 'aws-cdk-lib/aws-events';
import { Construct } from 'constructs';
import { ToolkitCleaner } from 'cloudstructs/lib/toolkit-cleaner';

export class CdkS3CleanerStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    new ToolkitCleaner(this, 'ToolkitCleaner', {
      retainAssetsNewerThan: cdk.Duration.days(7),
      schedule: Schedule.rate(cdk.Duration.days(30)),
    });
  }
}