From 893278d9c5cebba1f7545a5cff4622eefe842f5d Mon Sep 17 00:00:00 2001 From: Richard Keit Date: Thu, 20 Jul 2023 18:53:55 +1000 Subject: [PATCH] feat(bucketLifecycle): allow for prefix --- .../accelerator/lib/stacks/accelerator-stack.ts | 1 + .../accelerator/lib/stacks/logging-stack.ts | 2 +- .../@aws-accelerator/config/lib/common-types/types.ts | 2 ++ .../@aws-accelerator/constructs/lib/aws-s3/bucket.ts | 10 ++++++---- .../constructs/test/aws-s3/bucket.test.ts | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/source/packages/@aws-accelerator/accelerator/lib/stacks/accelerator-stack.ts b/source/packages/@aws-accelerator/accelerator/lib/stacks/accelerator-stack.ts index 38fd37225..5d6c04941 100644 --- a/source/packages/@aws-accelerator/accelerator/lib/stacks/accelerator-stack.ts +++ b/source/packages/@aws-accelerator/accelerator/lib/stacks/accelerator-stack.ts @@ -530,6 +530,7 @@ export abstract class AcceleratorStack extends cdk.Stack { expiration: lifecycleRule.expiration, expiredObjectDeleteMarker: lifecycleRule.expiredObjectDeleteMarker, id: lifecycleRule.id, + prefix: lifecycleRule.prefix, noncurrentVersionExpiration: lifecycleRule.noncurrentVersionExpiration, noncurrentVersionTransitions, transitions, diff --git a/source/packages/@aws-accelerator/accelerator/lib/stacks/logging-stack.ts b/source/packages/@aws-accelerator/accelerator/lib/stacks/logging-stack.ts index 711c9b533..ff2a9d914 100644 --- a/source/packages/@aws-accelerator/accelerator/lib/stacks/logging-stack.ts +++ b/source/packages/@aws-accelerator/accelerator/lib/stacks/logging-stack.ts @@ -1273,7 +1273,7 @@ export class LoggingStack extends AcceleratorStack { kmsKey: this.cloudwatchKey, logRetentionInDays: this.props.globalConfig.cloudwatchLogRetentionInDays, }; - + this.centralLogsBucket = new CentralLogsBucket(this, 'CentralLogsBucket', { s3BucketName: this.centralLogsBucketName, serverAccessLogsBucket: serverAccessLogsBucket, diff --git a/source/packages/@aws-accelerator/config/lib/common-types/types.ts b/source/packages/@aws-accelerator/config/lib/common-types/types.ts index 29eb82f72..247defac4 100644 --- a/source/packages/@aws-accelerator/config/lib/common-types/types.ts +++ b/source/packages/@aws-accelerator/config/lib/common-types/types.ts @@ -338,6 +338,7 @@ export const lifecycleRuleConfig = t.interface({ noncurrentVersionExpiration: optional(t.number), noncurrentVersionTransitions: optional(t.array(transition)), transitions: optional(t.array(transition)), + prefix: optional(t.string), }); export const resourcePolicyStatement = t.interface({ @@ -355,6 +356,7 @@ export class LifeCycleRule implements t.TypeOf { readonly noncurrentVersionExpiration: number = 366; readonly noncurrentVersionTransitions: Transition[] = []; readonly transitions: Transition[] = []; + readonly prefix: string = ''; } export const shareTargets = t.interface({ diff --git a/source/packages/@aws-accelerator/constructs/lib/aws-s3/bucket.ts b/source/packages/@aws-accelerator/constructs/lib/aws-s3/bucket.ts index e3024d439..6f1e78ca4 100644 --- a/source/packages/@aws-accelerator/constructs/lib/aws-s3/bucket.ts +++ b/source/packages/@aws-accelerator/constructs/lib/aws-s3/bucket.ts @@ -59,6 +59,7 @@ export interface S3LifeCycleRule { noncurrentVersionExpiration: number; transitions: Transition[]; noncurrentVersionTransitions: Transition[]; + prefix: string; } /** @@ -349,19 +350,20 @@ export class Bucket extends Construct { }; noncurrentVersionTransitions.push(noncurrentVersionTransitionsConfig); } - - this.lifecycleRules.push({ + const lifecycle = { abortIncompleteMultipartUploadAfter: cdk.Duration.days( lifecycleRuleConfig.abortIncompleteMultipartUploadAfter, ), enabled: lifecycleRuleConfig.enabled, expiration: cdk.Duration.days(lifecycleRuleConfig.expiration), + prefix: lifecycleRuleConfig.prefix, transitions, noncurrentVersionTransitions, noncurrentVersionExpiration: cdk.Duration.days(lifecycleRuleConfig.noncurrentVersionExpiration), expiredObjectDeleteMarker: lifecycleRuleConfig.expiredObjectDeleteMarker, - id: `LifecycleRule${this.props.s3BucketName}`, - }); + id: `LifecycleRule${lifecycleRuleConfig.prefix}${this.props.s3BucketName}`, + } + this.lifecycleRules.push(lifecycle); } } else { this.lifecycleRules.push({ diff --git a/source/packages/@aws-accelerator/constructs/test/aws-s3/bucket.test.ts b/source/packages/@aws-accelerator/constructs/test/aws-s3/bucket.test.ts index 69e7a97f2..69da70554 100644 --- a/source/packages/@aws-accelerator/constructs/test/aws-s3/bucket.test.ts +++ b/source/packages/@aws-accelerator/constructs/test/aws-s3/bucket.test.ts @@ -211,6 +211,7 @@ describe('Bucket', () => { s3LifeCycleRules: [ { id: '1', + prefix: 'object-prefix', abortIncompleteMultipartUploadAfter: 1, enabled: true, expiration: 24,