Sub topic 14
Building your own construct
This chapter assumes you know the App, Stack and construct tree model, the difference between L1, L2 and L3 constructs, and how to define an S3 bucket in TypeScript. A custom construct packages a group of resources and their defaults into one reusable class.
Why write a construct
Copying the same bucket settings into every stack invites drift. One stack gets encryption, another forgets it. A custom construct puts the settings in one place. Teams then share the class instead of a wiki page of rules. In the terms of the previous chapter, a custom construct is an L3 construct that you write yourself.
The anatomy of a construct
Every construct follows the same pattern:
- Extend
Constructfrom theconstructspackage. - Accept
scopeandidand pass them tosuper. - Accept an optional props interface for settings callers may change.
- Create child resources with
thisas their scope. - Expose the resources that other code needs as public readonly properties.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export interface SecureBucketProps {
readonly versioned?: boolean;
}
export class SecureBucket extends Construct {
public readonly bucket: s3.Bucket;
constructor(scope: Construct, id: string, props: SecureBucketProps = {}) {
super(scope, id);
this.bucket = new s3.Bucket(this, 'Bucket', {
versioned: props.versioned ?? true,
encryption: s3.BucketEncryption.S3_MANAGED,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
enforceSSL: true,
});
}
}The versioned prop defaults to true through ??. Encryption, public access blocking and SSL enforcement are fixed, so callers cannot weaken them by accident.
Using the construct
Inside a stack, you create it like any built-in construct. The public bucket property lets you pass the inner bucket to grant methods.
const uploads = new SecureBucket(this, 'Uploads');
uploads.bucket.grantRead(fn);The construct adds a level to the tree. The bucket's path becomes AppStack/Uploads/Bucket, and the logical ID in the template is derived from that path. Because the ID of the inner bucket is fixed inside the construct, you can reuse the construct many times in one stack as long as each instance gets a different id.
Design guidelines
- Expose what callers need and nothing more. A public
bucketproperty is enough for most cases. - Keep props small and optional, with safe defaults.
- Use
thisas the scope for child resources, never the outerscope. Otherwise the tree no longer shows the ownership. - Do not hard-code physical names. Let CDK generate them so two copies of the construct do not collide.
- Pass values between resources as properties, not as strings you build by hand. Properties carry tokens, which resolve at deploy time.
Sharing a construct
You can keep a construct in the same repository and import it. To share it across projects, publish it as a package in your registry, such as npm or PyPI. Consumers then install it like any library.
Exercise
Why does SecureBucket create its bucket with this as the scope instead of scope?
Show answer
Using this makes the bucket a child of the SecureBucket construct, so the tree path reads AppStack/Uploads/Bucket. This keeps ownership clear and prevents id collisions when the construct is used more than once. Using scope would attach the bucket to the parent and break both.