Understanding Constructs in the AWS CDK
Constructs are fundamental building blocks of the AWS Cloud Development Kit. They represent cloud components in the form of programmable classes. When AWS CDK apps are synthesized, these Constructs are translated into CloudFormation templates which AWS can then deploy.
What are Constructs?
Constructs are essentially classes that can be reused to create cloud resources. The stack that you define in your CDK app is a collection of initialised Constructs.
In previous posts, we created a new CDK App which used the Bucket
Construct to create an S3 bucket.
from aws_cdk import (
Stack,
aws_s3 as s3,
RemovalPolicy,
)
from constructs import Construct
class CdkAppStack(Stack):
BUCKET_ID = "MyFirstBucket"
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create an S3 bucket
s3.Bucket(self, id=self.BUCKET_ID) # This is a Construct
What are the different types of Constructs?
As shown in the diagram above, there are three types of Constructs:
- L1 Constructs: CFN Resources: These are low-level constructs that map directly to CloudFormation resources . Each L1 Construct represents one CloudFormation resource type, such as an S3 Bucket or an EC2 Instance. These L1 Constucts are the foundation of all other Constructs and are automatically generated from the CloudFormation Resource Specification.
Example:
from aws_cdk.aws_s3 import CfnBucket
bucket = CfnBucket(self, "MyBucket", bucket_name="my-bucket-name")
- L2 Constructs: Curated Constructs: These are higher-level abstractions that provide sensible defaults and ease-of-use. They encapsulate multiple L1 or L2 Constructs, providing additional functionality and a more intuitive interface. The difference between L1 and L2 Constructs is that L2 Constructs are created by AWS CDK developers and usually have certain defaults set. For example, the
Bucket
Construct is an L2 Construct that encapsulates theCfnBucket
L1 Construct. TheBucket
Construct automatically computes thebucket_name
based on app name and bucket ID.
Example:
from aws_cdk.aws_s3 import Bucket
bucket = Bucket(self, "MyBucket")
- L3 Constructs: Patterns: These are the highest-level abstractions that are not part of the core AWS CDK library. They are usually created by AWS CDK developers and are available as separate libraries. These Constructs are not part of the core AWS CDK library because they are not generic enough to be used by everyone.
Typically, L3 constructs are not part of the standar AWS CDK library and needs to be installed. You can find a list of available L3 constructs on the AWS Constructs Hub.
Creating your own Constructs
You can create your own Constructs by extending the Construct
class.
Let's create a custom Construct that creates an S3 bucket with a lifecycle policy that deletes objects after 30 days.
from aws_cdk import (
Stack,
aws_s3 as s3,
RemovalPolicy,
)
from constructs import Construct
class CustomBucket(Construct):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create an S3 bucket
bucket = s3.Bucket(self, id="MyBucket", removal_policy=RemovalPolicy.DESTROY)
# Add lifecycle policy to delete objects after 30 days
bucket.add_lifecycle_rule(expiration=Duration.days(30))
Now, we can use this custom Construct in our CDK app as follows:
from aws_cdk import (
Stack,
aws_s3 as s3,
RemovalPolicy,
)
from constructs import Construct
class CdkAppStack(Stack):
BUCKET_ID = "MyFirstBucket"
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create an S3 bucket
CustomBucket(self, id=self.BUCKET_ID)
You can deploy this app by running cdk deploy
. When you destroy the stack by running cdk destroy
.