DEV Community

Cover image for AWS-CDK Custom constructs
Yaseen
Yaseen

Posted on

1

AWS-CDK Custom constructs

CDK constructs are cloud components. We use constructs to encapsulate logic, which we can reuse throughout our infrastructure code.
AWS also defines Constructs as they are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS Cloud Formation needs to create the component.

import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import * as cdk from 'aws-cdk-lib';

export class s3BucketConstruct extends Construct {
  public readonly s3Bucket: s3.Bucket;

  public constructor(scope: Construct, id: string) {
    super(scope, id);

    this.s3Bucket = new s3.Bucket(scope, `sample-s3-bucket`, {
      // Block all public access
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
      // When stack is deleted, delete this bucket also
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      // Delete contained objects when bucket is deleted
      autoDeleteObjects: true,
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

This is how we can create custom construct.
You can also refer https://kuchbhilearning.blogspot.com/2022/09/aws-cdk-custom-constructs.html

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay