!Ref VS !GetAtt

The !GetAtt (Get Attribute) is a CloudFormation intrinsic function that helps you get the value of an attribute from a resource that has been created in your CloudFormation template.

In the example I showed earlier, !GetAtt NotificationFunction.Arn is getting the ARN (Amazon Resource Name) of a Lambda function named "NotificationFunction".

Here are some common uses of !GetAtt:

# Getting Lambda Function ARN
LambdaArn: !GetAtt MyLambdaFunction.Arn

# Getting S3 Bucket Domain Name
BucketDomainName: !GetAtt MyS3Bucket.DomainName

# Getting DynamoDB Table Stream ARN
StreamArn: !GetAtt MyDynamoTable.StreamArn

# Getting VPC ID
VpcId: !GetAtt MyVPC.VpcId

# Getting RDS Endpoint Address
DbEndpoint: !GetAtt MyRDSInstance.Endpoint.Address

The syntax is:

Where:

  • ResourceLogicalName: The logical name you gave to the resource in your template

  • AttributeName: The specific attribute you want to reference

This is particularly useful because:

  1. You don't need to hardcode values

  2. It ensures resources are properly linked together

  3. CloudFormation will automatically resolve the actual value during deployment

  4. It helps maintain dependencies between resources

For example, if you're creating a Lambda function and need to reference it in an EventBridge rule, you would:

!Ref

The !Ref (Reference) is another CloudFormation intrinsic function that allows you to refer to resources or parameters in your template. It's simpler than !GetAtt because it typically returns a default value for the resource.

For different resource types, !Ref returns different values:

  1. For SNS Topics -> Returns the Topic ARN

  2. For S3 Buckets -> Returns the Bucket name

  3. For Parameters -> Returns the parameter value

  4. For EC2 Instances -> Returns the instance ID

Here are some common examples:

In our previous EventBridge example:

The key differences between !Ref and !GetAtt:

  1. !Ref gets a default value:

  1. !GetAtt gets a specific attribute:

Last updated

Was this helpful?