When working with AWS CloudFormation nested stacks, you often need to pass resource attributes between stacks. For SNS topics, the ARN (Amazon Resource Name) is the critical identifier needed for cross-stack references.
The proper way to retrieve an SNS topic's ARN in CloudFormation templates is:
"Fn::GetAtt": ["MySNSTopic", "TopicArn"]
This differs from some other AWS resources because SNS specifically uses "TopicArn" as the attribute name rather than just "Arn".
Here's a complete example showing how to create an SNS topic and reference its ARN in another stack:
# Parent Stack
"MySNSTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"DisplayName": "MyNotificationTopic",
"TopicName": "MyProductionTopic"
}
},
"Outputs": {
"SNSTopicARN": {
"Description": "The ARN of the SNS topic",
"Value": {
"Fn::GetAtt": ["MySNSTopic", "TopicArn"]
}
}
}
# Child Stack
"Parameters": {
"SNSTopicARN": {
"Type": "String",
"Description": "ARN of the SNS topic"
}
},
"Resources": {
"MyLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Environment": {
"Variables": {
"TOPIC_ARN": {"Ref": "SNSTopicARN"}
}
},
# Other Lambda properties...
}
}
}
1. Incorrect Attribute Name: Using "Arn" instead of "TopicArn" will fail.
2. Export/Import Issues: Ensure proper Export/Import when crossing stack boundaries.
3. Circular Dependencies: Avoid situations where stacks reference each other.
For more complex scenarios with separate stacks, use Outputs and Exports:
# Stack A (SNS)
"Outputs": {
"SNSTopicExport": {
"Value": {"Fn::GetAtt": ["MySNSTopic", "TopicArn"]},
"Export": {"Name": "MyProject-SNS-TopicARN"}
}
}
# Stack B (ELB)
"Parameters": {
"NotificationTopicARN": {
"Type": "String",
"Default": ""
}
}
# Then in your ELB config:
"Properties": {
"NotificationConfiguration": {
"TopicARN": {"Ref": "NotificationTopicARN"}
}
}
1. Check CloudFormation stack Events tab for errors
2. Verify the exported value exists in CloudFormation Exports
3. Use AWS CLI to confirm the ARN format:
aws sns list-topics --query 'Topics[?contains(TopicArn,MyProductionTopic)]'
When working with AWS CloudFormation nested stacks, you often need to pass resource attributes between stacks. A common scenario involves creating an SNS topic in one stack and needing its ARN in another stack (like an ELB or application stack). The challenge is finding the correct syntax to retrieve the SNS ARN after creation.
For SNS topics, AWS provides specific attributes you can retrieve using the Fn::GetAtt
intrinsic function. The ARN is available as an attribute of the SNS topic resource.
\"Fn::GetAtt\" : [ \"MySNSTopic\", \"TopicArn\" ]
Here's a complete example showing how to pass the SNS ARN between nested stacks:
// Parent stack template
{
\"Resources\": {
\"MySNSTopicStack\": {
\"Type\": \"AWS::CloudFormation::Stack\",
\"Properties\": {
\"TemplateURL\": \"sns-template.json\",
\"Parameters\": {
\"TopicName\": \"MyNotificationTopic\"
}
}
},
\"MyELBStack\": {
\"Type\": \"AWS::CloudFormation::Stack\",
\"Properties\": {
\"TemplateURL\": \"elb-template.json\",
\"Parameters\": {
\"NotificationARN\": {\"Fn::GetAtt\": [\"MySNSTopicStack\", \"Outputs.TopicARN\"]}
}
}
}
}
}
// sns-template.json (child stack)
{
\"Resources\": {
\"MySNSTopic\": {
\"Type\": \"AWS::SNS::Topic\",
\"Properties\": {
\"TopicName\": {\"Ref\": \"TopicName\"}
}
}
},
\"Outputs\": {
\"TopicARN\": {
\"Description\": \"ARN of the SNS topic\",
\"Value\": {\"Fn::GetAtt\": [\"MySNSTopic\", \"TopicArn\"]}
}
}
}
For SNS topics, you can also use the Ref
function which returns the ARN by default:
\"Ref\" : \"MySNSTopic\"
This is equivalent to using Fn::GetAtt
with the \"TopicArn\" attribute.
- Make sure to define outputs in your nested stack if you need to access the ARN from the parent stack
- The ARN format will be:
arn:aws:sns:region:account-id:topic-name
- Cross-region references require additional configuration
If you're getting errors when trying to retrieve the ARN:
- Verify the logical name matches exactly (case-sensitive)
- Check that the SNS topic is properly defined in the template
- Ensure you're using the correct attribute name (\"TopicArn\")
- For nested stacks, confirm the output is properly defined