diff --git a/packages/aws-cdk-lib/aws-s3/lib/bucket.ts b/packages/aws-cdk-lib/aws-s3/lib/bucket.ts index d220e220082bb..221ef5d68819a 100644 --- a/packages/aws-cdk-lib/aws-s3/lib/bucket.ts +++ b/packages/aws-cdk-lib/aws-s3/lib/bucket.ts @@ -2085,6 +2085,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..6696adabf40f6 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, ExpirationDate, or TagFilters.', () => { + 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 ExpirationInDays, ExpirationDate, or TagFilters.', () => { + 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 ExpirationInDays, ExpirationDate, or 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();