Identifiers within AWS CDK

A quick blog today on both Construct and Unique identifiers within AWS CDK as well as objects within a CDK construct.

Objects in a CDK construct

When working within a Construct, AWS resources are referenced as objects when created. In the below screenshot, this.acme_vpc is an example of an object.

You can also see that I refer to this.acme_vpc object on line 25 to set the context of where a security group rule should be applied.

Construct ID’s and Unique ID’s

In the below screenshot, the second argument passed into this.acme_vpc is ‘ACMEVPC’.

ACMEVPC is a Construct ID and is unique to the scope of the stack it was created in. If you create multiple stacks based on this stack as a template, it is still okay as when you run a ‘cdk synth’ or ‘cdk deploy’ command, a Unique ID will be created. The Unique ID will generate an 8 digit has on the end of your Construct ID within your Cloudformation.

Below is a screenshot of the cloudformation generated with a ‘cdk synth’ command run on the acme_vpc we built in CDK above.

As mentioned above, if you had created multiple stacks based on this primary stack as a template, you would see the same construct id followed by the unique 8 digit hash for each resource making up the unique id within Cloudformation.

Thanks for reading, and if you’d like to dive deeper on Construct ID’s and Unique ID’s in particular, would recommend having a read of the AWS CDK Identifiers documentation which goes into more depth on the above: https://docs.aws.amazon.com/cdk/latest/guide/identifiers.html

– Matt Coles

Leave a comment

Your email address will not be published. Required fields are marked *