How to get Account ID and Region in AWS CDK

avatar

Borislav Hadzhiev

Fri Apr 23 20213 min read

Updated on Fri Apr 23 2021

Getting AccountId and Region in AWS CDK #

In order to access the accountId, region and availabilityZones properties in our CDK code, we have to set the env property when instantiating our Stack.

The code for this article is available on GitHub
import * as cdk from '@aws-cdk/core'; export class MyCdkStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); console.log('accountId: ', cdk.Stack.of(this).account); console.log('region: ', cdk.Stack.of(this).region); console.log('availability zones: ', cdk.Stack.of(this).availabilityZones); } } const app = new cdk.App(); new MyCdkStack(app, `my-cdk-stack-dev`, { stackName: `my-cdk-stack-dev`, // ๐Ÿ‘‡ now explicitly setting account and region env: { region: process.env.CDK_DEFAULT_REGION, account: process.env.CDK_DEFAULT_ACCOUNT, }, });

If we run the cdk synth command we can see that we are now able to access the accountId, region and availabilityZones at synthesis time in our CDK code:

env agnostic stack props

I have excluded the accountId property from the screenshot, but it too was resolved.

The recommended way by the CDK team to get access to the accountId and region properties is to use the Stack.of(this).account and Stack.of(this).region API.

Notice that in our code snippet above we used two environment variables that are available in our CDK environment:

{ region: process.env.CDK_DEFAULT_REGION, account: process.env.CDK_DEFAULT_ACCOUNT, }

The value of these environment variables depends on, whether we pass in a --profile flag when we issue the cdk deploy command.

For example, if we specify a profile, the values of CDK_DEFAULT_REGION and CDK_DEFAULT_ACCOUNT would correspond to the profile's region and account properties:

shell
npx cdk deploy --profile myprofile my-stack

If we don't set the --profile flag, the values would correspond to the default profile's region and account properties:

shell
npx cdk deploy my-stack

Either way, if we have the AWS CLI configured on our machine, by passing in the CDK_DEFAULT_REGION and CDK_DEFAULT_ACCOUNT properties we are explicitly setting the environment, which enables us to access the properties at synthesis time in our CDK code.

When collaborating with a team, on a project, don't use theCDK_DEFAULT_REGION and CDK_DEFAULT_ACCOUNT environment variables. The default profile of the different developers working on the project could be set to different regions and accounts.

Instead just pass the values as environment variables explicitly, you can hard code the values, manage them as secrets, etc.

Environment Agnostic Stacks in AWS CDK #

An environment-agnostic stack is a stack where we haven't explicitly set the account and region properties.

const app = new cdk.App(); new MyCdkStack(app, `my-cdk-stack-dev`, { stackName: `my-cdk-stack-dev`, // ๐Ÿ‘‡ we are not explicitly setting the region and account // env: { // region: 'us-east-1', // account: 123456789, // } });

Because we haven't explicitly set the env property in our stack, the account and region properties will be resolved by CloudFormation at deploy time rather than synthesis time.

Since we want access to these properties in our CDK code we have to find a way to resolve them at synthesis time.

Let's try to get the accountId, region and availabilityZones properties in an environment agnostic stack, without explicitly setting the env property:

import * as cdk from '@aws-cdk/core'; export class MyCdkStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); // ๐Ÿ‘‡ recommended way to get access to the properties console.log('accountId: ', cdk.Stack.of(this).account); console.log('region: ', cdk.Stack.of(this).region); console.log('availability zones: ', cdk.Stack.of(this).availabilityZones); } } const app = new cdk.App(); new MyCdkStack(app, `my-cdk-stack-dev`, { stackName: `my-cdk-stack-dev`, // ๐Ÿ‘‡ we are not explicitly setting the region and account // env: { // region: 'us-east-1', // account: 123456789, // } tags: {env: 'dev'}, });

We only get unresolved token values that will eventually be resolved at deploy time by CloudFormation.

env agnostic stack props

The solution is to explicitly set the account and region and call the Stack.of APIs:

console.log('accountId:', cdk.Stack.of(this).account); console.log('region', cdk.Stack.of(this).region); console.log('availability zones', cdk.Stack.of(this).availabilityZones);

Further Reading #

Add me on LinkedIn

I'm a Web Developer with TypeScript, React.js, Node.js and AWS experience.

Let's connect on LinkedIn

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee