Skip to content

CDK S3 Bucket growing out of control since using 'cdk gc --unstable=gc' - garbage collection

0

I'd noticed that the S3 bucket created by CDK was getting larger over time. My understanding is that this is created to hold temporary assets during deployment, and that for the most part those are no longer needed after deploy, for example, Lambda makes its own copy of the asset from the S3 bucket during deployment, so the CDK bucket object is no longer needed.

I explored how this was meant to be managed, and discovered the 'cdk gc --unstable=gc' command, which is meant to identify objects no longer needed, and mark the for deletion. Since issuing this command, the total size and count of objects has increased exponentially which is rather concerning.

I initially ran the 'gc' command at the end of December, and when I noticed that the object count/size increased I ran it again mid-January. As you can see from the metrics graph it seems to be going nuts. I'm actively developing a project which is involving multiple deploys per day, but that had been the case prior to using 'gc' so I don't think current activity explains it.

I checked the buckets lifecycle rules and these are still unmodified from the default (remove objects over 30 days).

What can I do to resolve this?
Is it ok to just delete all objects from the CDK bucket, or will that cause future deployments to either fail or be slow?!

Enter image description here

Enter image description here

1 Answer
0

Issue Analysis:

  • The CDK asset bucket is used to store temporary deployment assets (e.g., Lambda package files, CloudFormation templates).
  • Normally, older assets should be automatically deleted, but running cdk gc --unstable=gc appears to have caused a sharp increase in stored objects.
  • Lifecycle rules on the bucket were unchanged (default: deleting objects older than 30 days).
  • The issue persisted across multiple deployments, with object count and bucket size continuing to grow abnormally.

Potential Causes:

    • CDK Garbage Collection Behavior:
  • The cdk gc command may not be functioning as expected and could be leaving behind additional metadata or duplicate assets.

  • Asset Retention Settings in CDK:

  • If assets are not correctly marked for deletion, they may remain indefinitely.

    • Deployment Frequency Impact:
  • Since multiple deployments occur per day, new assets keep accumulating, exacerbating the issue.

  • Stale Assets Not Getting Cleaned Up:

  • CDK might not be correctly tracking which assets are in use, leading to excessive storage.

Solution & Workarounds:

  1. Manually Clean Up the Bucket (Safe Approach)
  • Before deleting anything, verify that the bucket contains only deployment artifacts and not any critical assets.
  • Use an S3 lifecycle policy to automatically delete objects after a shorter duration (e.g., 7 days instead of 30 days). Example policy:
{
    "Rules": [
        {
            "ID": "AutoDeleteOldCDKAssets",
            "Prefix": "",
            "Status": "Enabled",
            "Expiration": {
                "Days": 7
            }
        }
    ]
}

  • If immediate cleanup is needed, manually delete old assets via AWS CLI:
aws s3 rm s3://your-cdk-bucket-name --recursive

Be careful not to delete assets currently in use.

  1. Adjust CDK Asset Retention Policy

Ensure that cdk destroy properly cleans up resources. Run the following command to check retained stacks:

cdk list

Destroy unused stacks:

cdk destroy stack-name

  1. Investigate and Use Stable CDK Commands

Instead of cdk gc --unstable=gc, use:

cdk synth && cdk deploy

If garbage collection is required, test with the latest stable CDK version.

  1. Upgrade CDK to the Latest Version

This issue might be a bug in a specific CDK version. Try upgrading:

npm install -g aws-cdk

Verify the installed version:

cdk --version

  1. Monitor Bucket Growth Over Time
  • Use CloudWatch metrics to track bucket size (BucketSizeBytes) and object count (NumberOfObjects).
  • Set up an alert for unexpected growth trends.

Final Thoughts

  • It is generally safe to delete unnecessary objects from the CDK asset bucket if you are sure they are not in use.
  • Enabling a strict lifecycle policy for automatic cleanup is recommended.
  • AWS CDK's gc behavior should be used cautiously, as it may not reliably remove old assets.
EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.