How to Get Attribute [GetAtt] in AWS CDK

avatar

Borislav Hadzhiev

Sat Apr 24 20213 min read

banner

Photo by Faye Cornish

Updated on Sat Apr 24 2021

GetAtt in CDK #

The GetAtt function returns the value of a specific attribute.

GetAtt is a function, that comes from CloudFormation, but since our CDK code gets compiled down to CloudFormation before a deployment, we can use this feature in CDK as well.

We can use the GetAtt function in CDK in two ways:

In this article we are going to demonstrate how to use GetAtt both ways. I'll create a simple CDK stack consisting of a single S3 bucket.

GetAtt directly on the CfnResource #

The code for this article is available on GitHub

First, we'll use the getAtt function directly on the CfnResource:

lib/cdk-starter-stack.ts
import * as s3 from '@aws-cdk/aws-s3'; 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); const s3Bucket = new s3.Bucket(this, id); const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket; console.log('directly ๐Ÿ‘‰', cfnBucket.getAtt('Arn').toString()); } }

In the code snippet, we:

  1. Defined an S3 bucket using the level 2 Bucket construct

  2. Used the node.defaultChild property and casted the type to a CfnBucket so we can access the getAtt method

  3. Logged the call to getAtt as a string

If we synth the stack at this point we'll see that the value resolves to a Token.

Token output

In short a token is an encoded value, that will be resolved by CloudFormation at deployment time. If you want to read more on tokens I have written an article - What is a Token in CDK.

GetAtt using the Fn class #

lib/cdk-starter-stack.ts
import * as s3 from '@aws-cdk/aws-s3'; 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); // ...rest console.log( 'on Fn class ๐Ÿ‘‰', cdk.Fn.getAtt(cfnBucket.logicalId, 'Arn').toString(), ); // ๐Ÿ‘‡ add the call to getAtt as an Output new cdk.CfnOutput(this, 'bucketArn', { value: cfnBucket.getAtt('Arn').toString(), description: 'The arn of the s3 bucket', exportName: 'avatarsBucket', }); } }

In the code snippet, we:

  1. Used the getAtt static method on the Fn class. The method takes the CloudFormation logical ID of the resource as the first parameter and the name of the attribute as the second.

  2. We've created an Output and specified the bucket's arn from the call to getAtt as the value.

The output from the console.log statement is a Token, that will get resolved by CloudFormation at deployment time:

fn class token

Next, we'll deploy the stack and see if the call to cfnBucket.getAtt resolves in the Outputs section:

shell
npx cdk deploy \ --outputs-file ./cdk-outputs.json

After I've executed the deploy command with the outputs redirected to a file on the local file system, the contents of cdk-outputs.json look like:

CDK outputs

We can see that the call to cfnBucket.getAtt successfully got resolved, by CloudFormation at deployment time.

The cdk.out directory is where the CDK CLI stores file assets in preparation for deployment. If we take a look at the synthesized CloudFormation template, we can see the use of the intrinsic GetAtt function:

cdk.out/my-cdk-stack.template.json
{ "Outputs": { "bucketArn": { "Description": "The arn of the s3 bucket", "Value": { // ๐Ÿ‘‡ Uses GetAtt intrinsic function "Fn::GetAtt": [ "mycdkstack4E08F0DD", "Arn" ] }, "Export": { "Name": "avatarsBucket" } } } }

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