diff --git a/aws/aspects/ConfigureDeletionPolicy.ts b/aws/aspects/ConfigureDeletionPolicy.ts new file mode 100644 index 00000000..16b95dce --- /dev/null +++ b/aws/aspects/ConfigureDeletionPolicy.ts @@ -0,0 +1,12 @@ +import { IAspect, RemovalPolicy, aws_s3 } from 'aws-cdk-lib' +import { IConstruct } from 'constructs' + +export class ConfigureDeletionPolicy implements IAspect { + public visit(node: IConstruct): void { + if (!(node instanceof aws_s3.CfnBucket)) { + return + } + + node.applyRemovalPolicy(RemovalPolicy.RETAIN) + } +} diff --git a/aws/aspects/HTTPApiGatewayLogs.ts b/aws/aspects/HTTPApiGatewayLogs.ts index eb6b41fe..65d7bc24 100644 --- a/aws/aspects/HTTPApiGatewayLogs.ts +++ b/aws/aspects/HTTPApiGatewayLogs.ts @@ -1,4 +1,4 @@ -import { IAspect, Tags, aws_apigatewayv2, aws_iam, aws_logs } from 'aws-cdk-lib' +import { IAspect, aws_apigatewayv2, aws_iam, aws_logs } from 'aws-cdk-lib' import { RetentionDays } from 'aws-cdk-lib/aws-logs' import { Construct, IConstruct } from 'constructs' @@ -11,7 +11,6 @@ export class HTTPApiGatewayLogs implements IAspect { } const scope = new Construct(node.node.scope!, 'logs') - Tags.of(scope).add('custom:aspect', HTTPApiGatewayLogs.name) const group = new aws_logs.LogGroup(scope, `group`, { logGroupName: `/${node.node.scope!}`, diff --git a/aws/stacks/Observability.ts b/aws/stacks/Observability.ts index bdc743dd..1f10684f 100644 --- a/aws/stacks/Observability.ts +++ b/aws/stacks/Observability.ts @@ -1,5 +1,6 @@ import { Aspects, Stack, aws_s3 } from 'aws-cdk-lib' import { Construct, IConstruct } from 'constructs' +import { ConfigureDeletionPolicy } from '../aspects/ConfigureDeletionPolicy' import { EnableLambdaXRayTracing } from '../aspects/EnableLambdaXRayTracing' import { HTTPApiGatewayLogs } from '../aspects/HTTPApiGatewayLogs' import { S3BucketAccessLogs } from '../aspects/S3BucketAccessLogs' @@ -18,9 +19,14 @@ export class ObservabilityStack extends Stack { this.enableS3AccessLogs(node) this.enableLambdaXRayTracing(node) this.enableHttpApiGatewayLogs(node) + this.configureDeletionPolicy(node) }) } + public configureDeletionPolicy(node: Construct) { + Aspects.of(node).add(new ConfigureDeletionPolicy()) + } + public enableHttpApiGatewayLogs(node: Construct) { Aspects.of(node).add(new HTTPApiGatewayLogs()) }