What is Fn::GetAtt in CloudFormation: Simple Explanation and Example
Fn::GetAtt is a CloudFormation function that retrieves the value of an attribute from a resource in your template. It helps you get details like resource IDs or endpoints created by other resources so you can connect or configure them together.How It Works
Imagine you build a house and want to know the address to share with your friends. In CloudFormation, when you create resources like servers or databases, each has useful details called attributes, such as an ID or URL. Fn::GetAtt is like asking the builder for the exact address or property detail of a resource you created.
It works by specifying the resource name and the attribute you want. CloudFormation then looks up that resource and returns the requested attribute value. This lets you connect resources easily, like linking a server to a database by using the database's endpoint address.
Example
This example shows how to get the endpoint address of an AWS RDS database instance using Fn::GetAtt. The endpoint is needed to connect an application to the database.
Resources:
MyDBInstance:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.t3.micro
Engine: mysql
MasterUsername: admin
MasterUserPassword: password123
MyAppServer:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0abcdef1234567890
InstanceType: t2.micro
UserData:
Fn::Base64: !Sub |
#!/bin/bash
echo "DB Endpoint: ${DBEndpoint}"
Outputs:
DBEndpoint:
Description: "The database endpoint address"
Value: !GetAtt MyDBInstance.Endpoint.AddressWhen to Use
Use Fn::GetAtt when you need to get specific details from one resource to use in another. For example, you might want the IP address of a server, the URL of a load balancer, or the ARN (Amazon Resource Name) of a role.
This is common when resources depend on each other, like an application server needing the database endpoint to connect, or a security group needing the ID of a network interface.
Key Points
- Fn::GetAtt fetches attributes from resources in the same CloudFormation stack.
- It requires the resource logical name and the attribute name.
- Common attributes include IDs, endpoints, ARNs, and URLs.
- It helps link resources by sharing important details automatically.
Key Takeaways
Fn::GetAtt retrieves specific attributes from resources in your CloudFormation template.