Skip to content

Commit

Permalink
feat: use aspects to configure deletion policy
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn committed Mar 26, 2024
1 parent 9963987 commit 365d78c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions aws/aspects/ConfigureDeletionPolicy.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 1 addition & 2 deletions aws/aspects/HTTPApiGatewayLogs.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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!}`,
Expand Down
6 changes: 6 additions & 0 deletions aws/stacks/Observability.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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())
}
Expand Down

0 comments on commit 365d78c

Please sign in to comment.