aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.57k stars 3.88k forks source link

(@aws-cdk/aws-s3): Add validation for lifecycleRules #22103

Open Damon1024 opened 2 years ago

Damon1024 commented 2 years ago

Describe the bug

When a bucket is initialised without either transitionDate or transitionAfter being set in lifecycleRules, the stack will build successfully but fail to deploy. Similarly, if both transitionDate and transitionAfter are set, it will fail to deploy as well.

Expected Behavior

Should throw an error when both transitionDate and transitionAfter are undefined, or both are defined.

Current Behavior

Build successfully, and got errors below during deployment

SampleBucket: creating CloudFormation changeset...
1:56:12 pm | CREATE_FAILED        | AWS::S3::Bucket    | sampleBucket
Exactly one of TransitionDate and TransitionInDays must be specified

 ❌  SampleBucket failed: Error: The stack named sampleBucket failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE: Exactly one of TransitionDate and TransitionInDays must be specified
    at prepareAndExecuteChangeSet (/Users/cdk/sample/node_modules/aws-cdk/lib/api/deploy-stack.ts:386:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at deployStack2 (/Users/cdk/sample/node_modules/aws-cdk/lib/cdk-toolkit.ts:240:24)
    at /Users/cdk/sample/node_modules/aws-cdk/lib/deploy.ts:39:11
    at run (/Users/cdk/sample/node_modules/p-queue/dist/index.js:163:29)

 ❌ Deployment failed: Error: Stack Deployments Failed: Error: The stack named sampleBucket failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE: Exactly one of TransitionDate and TransitionInDays must be specified
    at deployStacks (/Users/cdk/sample/node_modules/aws-cdk/lib/deploy.ts:61:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at CdkToolkit.deploy (/Users/cdk/sample/node_modules/aws-cdk/lib/cdk-toolkit.ts:312:7)
    at initCommandLine (/Users/cdk/sample/node_modules/aws-cdk/lib/cli.ts:349:12)

Stack Deployments Failed: Error: The stack named sampleBucket failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE: Exactly one of TransitionDate and TransitionInDays must be specified

Reproduction Steps

Deploy the stack below

import { Stack, StackProps, aws_s3 as s3 } from "aws-cdk-lib";
import { Construct } from "constructs";

export class TestStack extends Stack {
  bucket: s3.Bucket;

  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    this.bucket = new s3.Bucket(this, "test-s3-bucket", {
      lifecycleRules: [
        {
          transitions: [{ storageClass: s3.StorageClass.GLACIER }],
        },
      ],
    });
  }
}

Possible Solution

Add validation to cdk

Additional Information/Context

No response

CDK CLI Version

2.41.0

Framework Version

2.41.0

Node.js Version

16.13.1

OS

Linux

Language

Typescript

Language Version

TypeScript(4.7.4)

Other information

No response

peterwoodworth commented 2 years ago

Thanks for the report and your PR @Damon1024, this is definitely something we should try to throw at synth. We've given an initial review to the PR, check it out so we can try to merge this soon! πŸ˜„