How to get or set the Stack Name in AWS CDK

avatar
Borislav Hadzhiev

Last updated: Jan 26, 2024
3 min

banner

# Table of Contents

  1. Getting the Stack Name in AWS CDK
  2. Explicitly setting the Stack Name in AWS CDK

# Getting the Stack Name in AWS CDK

To get the name of a stack in CDK we have to access the stackName property on the core Stack class.

The code for this article is available on GitHub
lib/cdk-starter-stack.ts
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); console.log('stackName ๐Ÿ‘‰', cdk.Stack.of(this).stackName); } } const app = new cdk.App(); new MyCdkStack(app, 'my-cdk-stack');

We accessed the stackName property on the Stack class.

The Stack.of static method returns the Stack object, within which a construct is defined. The method provides a clean way to get a hold of the Stack object.

I'll run the CDK code by issuing the synth command.

shell
npx aws-cdk synth

The output shows that we've successfully retrieved the stack name.

synth class name

CDK resolves the stackName property on the Stack class at synthesis time, so we are able to access it in our code.

We didn't provide the stackName prop when instantiating our Stack, so the stack name was inferred from the id prop we passed to the Stack constructor.

We can explicitly set the stack name in the following way.

bin/cdk-starter.ts
const app = new cdk.App(); new MyCdkStack(app, 'my-cdk-id', { // ๐Ÿ‘‡ explicitly set stackName prop stackName: 'explicit-stack-name', });

I'll now run the synth command to see the output from the console.log call.

shell
npx aws-cdk synth

The output shows that we've successfully overridden the stack name.

new stack name

# Accessing the AWS::StackName Parameter

If you need to access the AWS::StackName pseudo parameter from CloudFormation, you can use Aws.STACK_NAME.

lib/cdk-starter-stack.ts
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); console.log('stackName param ๐Ÿ‘‰', cdk.Aws.STACK_NAME); } }

I'll synth to see the output from the console.log call.

shell
npx aws-cdk synth

The STACK_NAME property on the Aws class resolves at deployment time, rather than at synthesis time, so we get a Token value.

aws stack name

Tokens in CDK are encoded values that get resolved by CloudFormation at deployment time. I've written an article on the topic - What is a Token in AWS CDK

# Explicitly setting the Stack Name in AWS CDK

To explicitly set the stack name in CDK, we have to provide the stackName prop when creating the Stack.

import * as cdk from 'aws-cdk-lib'; const app = new cdk.App(); new MyCdkStack(app, 'my-construct-id', { // ๐Ÿ‘‡ explicitly setting stack name stackName: `my-cdk-stack`, });

For Python users - the property is named stack_name, instead of stackName.

If we now deploy our stack by running npx aws-cdk deploy, the name is set to my-cdk-stack.

with stack name

# Accessing the Stack Name in AWS CDK

To get the name of a CDK Stack, we have to access the stackName property on the Stack class.

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); console.log('stackName ๐Ÿ‘‰ ', cdk.Stack.of(this).stackName); } }

The output from the console.log call shows the stack name:

cdk stack name

# Default Behavior Without setting Stack Name

The default behavior is that if we deploy a stack without providing the stackName property, CDK sets the stack name to be inferred to the construct id of the stack.

const app = new cdk.App(); new MyCdkStack(app, 'my-construct-id');

In the scenario above, our stack will be named my-construct-id.

without stack name

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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 ยฉ 2024 Borislav Hadzhiev