!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.AddressThe 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:
You don't need to hardcode values
It ensures resources are properly linked together
CloudFormation will automatically resolve the actual value during deployment
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:
For SNS Topics -> Returns the Topic ARN
For S3 Buckets -> Returns the Bucket name
For Parameters -> Returns the parameter value
For EC2 Instances -> Returns the instance ID
Here are some common examples:
In our previous EventBridge example:
The key differences between !Ref and !GetAtt:
!Refgets a default value:
!GetAttgets a specific attribute:
Last updated
Was this helpful?