Sub topic 4
L1, L2 and L3 constructs
This chapter assumes you can already create a CDK project and know that every CDK object is a construct in a tree of App, Stack and constructs. Here you will learn the three levels of constructs and how to choose between them.
Three levels of abstraction
Constructs come in three levels. Each level sits on top of the one below it.
| Level | Also called | Example class | What it gives you |
|---|---|---|---|
| L1 | Cfn resources | `s3.CfnBucket` | A one-to-one mapping to a CloudFormation resource. Nothing is added for you. |
| L2 | Curated constructs | `s3.Bucket` | Sensible defaults, helper methods and grant methods. |
| L3 | Patterns | `ApplicationLoadBalancedFargateService` | Several connected resources for a common architecture. |
L1: exactly what CloudFormation offers
L1 classes are generated from the CloudFormation resource specification. Their names start with Cfn, and their properties match the CloudFormation properties. Because they are generated, a new CloudFormation feature is available as an L1 construct early.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class L1Stack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.CfnBucket(this, 'RawBucket', {
versioningConfiguration: { status: 'Enabled' },
});
}
}An L1 construct has no grantRead method, and it applies no encryption or other defaults. You write every property yourself.
L2: the level you will use most
L2 constructs wrap one or more L1 resources behind a friendlier interface. The s3.Bucket class from the previous chapters is an L2 construct. It lets you write versioned: true instead of a nested configuration object. It also provides methods such as grantRead, which generate IAM policies for you.
You can still reach the L1 resource underneath. This is called an escape hatch, and you use it when an L2 construct does not expose a property you need:
const bucket = new s3.Bucket(this, 'Uploads');
const cfnBucket = bucket.node.defaultChild as s3.CfnBucket;
cfnBucket.addPropertyOverride('AccelerateConfiguration.AccelerationStatus', 'Enabled');L3: whole architectures
L3 constructs, also called patterns, combine several L2 constructs into a working design. The following example creates a Fargate service behind an Application Load Balancer. By default it also creates a VPC, an ECS cluster, a load balancer, a listener, a target group, security groups and IAM roles:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as patterns from 'aws-cdk-lib/aws-ecs-patterns';
export class WebStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new patterns.ApplicationLoadBalancedFargateService(this, 'Web', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
});
}
}Choosing a level
- Start with L2. It gives the best balance of control and convenience.
- Use L3 when your needs match the pattern. Run
cdk difffirst, because a pattern can create resources that cost money. - Use L1 when no L2 construct exists, or when you need a property it does not expose.
- Prefer an escape hatch on an L2 construct over rewriting everything as L1.
Exercise
You need to set a CloudFormation property that the L2 s3.Bucket construct does not expose. What can you do without abandoning the L2 construct?
Show answer
Get the underlying L1 resource with bucket.node.defaultChild, cast it to s3.CfnBucket, and call addPropertyOverride (or set the property directly). This escape hatch keeps the L2 defaults and grant methods.