amazon-web-servicesamazon-s3deploymentaws-cdks3-lifecycle-policy

AWS CDK Change S3 deployment folder lifecycle time


When using the AWS CDK to deploy to AWS, the CDK uploads all the assets to an S3 bucket first, and then deploys to AWS resources accordingly.

That bucket can be seen in S3 (named 'cdk-...') and it has a lifecycle policy called 'CleanupOldVersions' which is automatically created when you first sync the CDK and that bucket is created. The lifecycle retains files for 365 days.

Is there a way to update the number of days in my CDK settings?


Solution

  • The lifecycle retains files for 365 days.

    This is incorrect. Files are retained indefinitely - the lifecycle rule only applies to non-current versions of the files. e.g. if you overwrite a file, its previous version(s) will be retained for 365 days.

    You can see this called out explicitly in the bootstrap stack template:

      LifecycleConfiguration:
        Rules:
          # Exising objects will never be overwritten but Security Hub wants this rule to exist
          - Id: CleanupOldVersions
            Status: Enabled
            NoncurrentVersionExpiration:
              NoncurrentDays: 365
    

    That said, if you would like to change this for any reason, you would need to provide your own CloudFormation template file and call cdk boostrap with the --template flag, pointing it to your custom template:

    cdk bootstrap --template my-custom-template.yaml

    For details, refer to the documentation for AWS::S3::Bucket NoncurrentVersionExpiration and to the CDK CLI reference for the bootstrap command