How to Override Logical IDs of Resources in AWS CDK

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

# Overriding Logical IDs in CDK

Logical IDs serve as unique resource identifiers.

Logical IDs are alphanumeric (A-Za-z0-9) and must be unique in a template.

We set the resource's logical ID in CloudFormation like so:

template.yaml
Resources: # ๐Ÿ‘‡ logical id MyLogicalId: Type: AWS::DynamoDB::Table Properties: # ... properties of the table

If we look at a deployed stack, we can see the Logical IDs of resources in the first column:

cloudformation logical ids

In order to override the Logical ID of a resource in CDK, we have to use the overrideLogicalId method on the CfnResource.

The code for this article is available on GitHub

To be more specific, if we had a CfnBucket resource we would use its overrideLogicalId method.

In order to demo the process, I'll create a CDK stack, which consists of a single S3 bucket:

lib-cdk-starter-stack.ts
import * as s3 from 'aws-cdk-lib/aws-s3'; import * as cdk from 'aws-cdk-lib'; export class MyCdkStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props: cdk.StackProps) { super(scope, id, props); const s3Bucket = new s3.Bucket(this, 'avatars-bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY, }); // ๐Ÿ‘‡ Get access to the CfnBucket resource const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket; // ๐Ÿ‘‡ Override the bucket's logical ID cfnBucket.overrideLogicalId('myLogicalId'); } }

In the code sample:

  1. We defined an S3 bucket using the Level 2 Bucket construct.

  2. We got access to the Level 1 CfnBucket resource and casted the type of the value to CfnBucket.

  3. We overrode the logical ID of the bucket to myLogicalId

Note that Logical IDs must be alphanumeric. For example, if we tried to set a Logical ID of my-logical-id we would get an error: "Resource namemy-logical-id is non alphanumeric."

If we take a look at the synthesized CloudFormation template in the cdk.out directory, we can see that the Logical ID we've provided is reflected in the CloudFormation template:

cdk out logical id

Let's look at the Logical ID of the S3 bucket in the CloudFormation console:

overridden logical id

We can see that the Logical ID of the bucket has been updated to the value we provided - myLogicalId.

Changing the logical ID of a resource causes resource replacement. The old resource gets deleted and a new one with the new logical ID gets created.

If I were to change the logical ID of the bucket:

lib/cdk-starter-stack.ts
- cfnBucket.overrideLogicalId('myLogicalId'); + cfnBucket.overrideLogicalId('anotherLogicalId');

And now run the diff command:

shell
npx aws-cdk diff

The output shows that if I were to run a deployment, my bucket with logical ID of myLogicalId would get deleted and a new bucket with logical ID of anotherLogicalId would get created:

changed logical id

We should be mindful about identifiers in CDK, especially when working with stateful resources like S3 buckets and databases. I've written an article - What is an identifier in AWS CDK if you want to read more on the topic.

# Further Reading

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev