diff --git a/packages/aws-cdk-lib/aws-s3/lib/bucket.ts b/packages/aws-cdk-lib/aws-s3/lib/bucket.ts index df198dafe9500..9db166b0beb6d 100644 --- a/packages/aws-cdk-lib/aws-s3/lib/bucket.ts +++ b/packages/aws-cdk-lib/aws-s3/lib/bucket.ts @@ -2119,6 +2119,11 @@ export class Bucket extends BucketBase { function parseLifecycleRule(rule: LifecycleRule): CfnBucket.RuleProperty { const enabled = rule.enabled ?? true; + if ((rule.expiredObjectDeleteMarker) + && (rule.expiration || rule.expirationDate || self.parseTagFilters(rule.tagFilters))) { + // ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays, ExpirationDate, or TagFilters. + throw new Error('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.'); + } const x: CfnBucket.RuleProperty = { // eslint-disable-next-line max-len diff --git a/packages/aws-cdk-lib/aws-s3/test/rules.test.ts b/packages/aws-cdk-lib/aws-s3/test/rules.test.ts index b218a098d4ff4..25a45a6ea344f 100644 --- a/packages/aws-cdk-lib/aws-s3/test/rules.test.ts +++ b/packages/aws-cdk-lib/aws-s3/test/rules.test.ts @@ -25,6 +25,51 @@ describe('rules', () => { }); }); + test('ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays.', () => { + const stack = new Stack(); + new Bucket(stack, 'Bucket', { + lifecycleRules: [{ + expiration: Duration.days(30), + expiredObjectDeleteMarker: true, + }], + }); + + expect(() => { + Template.fromStack(stack).toJSON(); + }).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.'); + }); + + test('ExpiredObjectDeleteMarker cannot be specified with ExpirationDate.', () => { + const stack = new Stack(); + new Bucket(stack, 'Bucket', { + lifecycleRules: [{ + expirationDate: new Date('2018-01-01'), + expiredObjectDeleteMarker: true, + }], + }); + + expect(() => { + Template.fromStack(stack).toJSON(); + }).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.'); + }); + + test('ExpiredObjectDeleteMarker cannot be specified with TagFilters.', () => { + const stack = new Stack(); + new Bucket(stack, 'Bucket', { + lifecycleRules: [{ + tagFilters: [ + { Key: 'tagname1', Value: 'tagvalue1' }, + { Key: 'tagname2', Value: 'tagvalue2' }, + ], + expiredObjectDeleteMarker: true, + }], + }); + + expect(() => { + Template.fromStack(stack).toJSON(); + }).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.'); + }); + test('Can use addLifecycleRule() to add a lifecycle rule', () => { // GIVEN const stack = new Stack();