DEV Community

Cover image for How to create an AWS CDK Construct
Danny Steenman for AWS Community Builders

Posted on • Originally published at towardsthecloud.com on

How to create an AWS CDK Construct

What is an AWS CDK Construct

A construct represents a cloud component. Constructs encapsulate everything that AWS CloudFormation needs to create the resources and properties. It can contain one or more AWS resources, you're free to customize it yourself.

The advantage of creating a construct is that you can re-use the components in stacks without redefining the code and simply importing it as a Class.

The following code snippet creates an AWS CDK construct scaffold which you can use as a template to define your cloud components.

AWS CDK Construct example

```typescript showLineNumbers
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

export interface ExampleConstructProps {
//insert properties you wish to expose
}

export class ExampleConstruct extends Construct {
constructor(scope: Construct, id: string, props: ConstructorNameProps) {
super(scope, id);
//Insert the AWS components you wish to integrate
}
}




### Explanation

Constructs are implemented in classes that extend the Construct base class. . All constructs take three parameters when they are initialized:

* **Scope** – The construct within which this construct is defined. You should usually pass this for the scope, because it represents the current scope in which you are defining the construct.
* **id** – An identifier that must be unique within this scope. The identifier serves as a namespace for everything that's defined within the current construct and is used to allocate unique identities such as resource names and AWS CloudFormation logical IDs.
* **Props** – A set of properties that define the construct's initial configuration.

## How to create an AWS CDK Construct

### Install packages

[Install AWS CDK](https://towardsthecloud.com/install-aws-cdk) and the AWS CDK v2 library in your project using yarn



```bash
yarn add aws-cdk-lib construct
yarn add -D aws-cdk
Enter fullscreen mode Exit fullscreen mode

Import the Construct Class

Import the newly created construct in your CDK App or Stack

import { ExampleConstruct } from './lib/construct-name';
Enter fullscreen mode Exit fullscreen mode

Instantiate a new Construct

The following example shows how you can instantiate an instance of the construct that we extended from the base class.

```typescript showLineNumbers
import { ExampleConstruct } from './lib/construct-name';

new ExampleConstruct(this, 'newConstruct', {
//insert props which you exposed in the interface ExampleConstructProps
});




## Learn more about AWS CDK

If you are a beginner or just starting out with AWS CDK then have a look at this article where you learn [what AWS CDK is](https://towardsthecloud.com/aws-cdk) and how it can improve your development cycle.

## Elevate Your AWS CDK App with Expert Review & Guidance

Unlock the full potential of your AWS CDK app with our Expert AWS CDK App Code Review Service, conveniently delivered through AWS IQ.

Gain invaluable insights, minimize risks, and set a clear path forward for your project's success.

[Schedule Your CDK Review Now](https://towardsthecloud.com/services/aws-cdk-review)

---
> Join my [newsletter](https://towardsthecloud.com/newsletter?ref=devto) for real-world insights, actionable strategies, and lessons learned the hard way on my journey building a successful [AWS Cloud Consulting business](https://towardsthecloud.com?ref=devto), delivered to your inbox.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)