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".
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:
Resources:
# Creating an SNS Topic
MyTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: my-notification-topic
# Creating an SQS Queue
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: my-queue
# Using !Ref to reference these resources
MyLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Environment:
Variables:
TOPIC_ARN: !Ref MyTopic # Gets the Topic ARN
QUEUE_URL: !Ref MyQueue # Gets the Queue URL
# Using with Parameters
Parameters:
EnvironmentName:
Type: String
Default: dev
# Using !Ref with a parameter
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref EnvironmentName # Gets the parameter value