Borislav Hadzhiev
Last updated: Apr 14, 2022
Check out my new book
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:
CfnResource
, by calling the
getAtt function
and passing in the attribute nameIn 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.
First, we'll use the getAtt
function directly on the CfnResource
:
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, id); const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket; console.log('directly 👉', cfnBucket.getAtt('Arn').toString()); } }
In the code snippet, we:
Defined an S3 bucket using the level 2 Bucket construct
Used the node.defaultChild
property and casted the type to a CfnBucket
so
we can access the getAtt
method
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.
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.
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); // ...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:
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.
We 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:
Let's deploy the stack and see if the call to cfnBucket.getAtt
resolves in the
Outputs
section:
npx aws-cdk deploy \ --outputs-file ./cdk-outputs.json
After I've ran the deploy
command with the outputs redirected to a file on the
local file system, the contents of cdk-outputs.json
look like:
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:
{ "Outputs": { "bucketArn": { "Description": "The arn of the s3 bucket", "Value": { // 👇 Uses GetAtt intrinsic function "Fn::GetAtt": [ "mycdkstack4E08F0DD", "Arn" ] }, "Export": { "Name": "avatarsBucket" } } } }