gqNtZGzNAhCjcnBszQhR

CDK Annotation Error Stack

Jan 6th, 2022 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2. import { Annotations, App as AwsApp, AppProps, Aspects, IAspect, Stack, StackProps } from 'aws-cdk-lib';
  3. import { Bucket, BucketEncryption, CfnBucket } from 'aws-cdk-lib/aws-s3';
  4. import { Construct, IConstruct } from 'constructs';
  5.  
  6. class BucketEncryptionAspect implements IAspect {
  7.   public visit(node: IConstruct): void {
  8.     if (node instanceof CfnBucket) {
  9.       if (!node.bucketEncryption) {
  10.         Annotations.of(node).addError(
  11.           `S3 bucket must be encrypted: ${node.bucketName}`
  12.         );
  13.       }
  14.     }
  15.   }
  16. }
  17.  
  18. class App extends AwsApp {
  19.   constructor(props?: AppProps) {
  20.     super(props);
  21.  
  22.     Aspects.of(this).add(new BucketEncryptionAspect());
  23.   }
  24. }
  25.  
  26. class ExampleStack extends Stack {
  27.   constructor(scope: Construct, id: string, props?: StackProps) {
  28.     super(scope, id, props);
  29.  
  30.     new Bucket(this, 'ExampleStackBucket', {
  31.         bucketName: 'example-stack-bucket',
  32.         encryption: BucketEncryption.UNENCRYPTED,
  33.     });
  34.   }
  35. }
  36.  
  37. const app = new App();
  38. new ExampleStack(app, 'ErrorExampleStack');
  39.  
Add Comment
Please, Sign In to add comment