How to Import an SNS Topic into a Nested CloudFormation Stack?
When working with AWS CloudFormation, you might find yourself needing to import existing resources into your infrastructure-as-code setup. In this guide, we'll walk through importing an SNS topic into a nested CloudFormation stack.
The Scenario
You have a CloudFormation template with a nested stack structure:
- Main Template (template.yaml): Contains the parent stack that references a nested stack
- Nested Template (nested-templates/sns-stack.yaml): Contains the SNS topic resources.
Main template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template with Nested Stacks
Parameters:
Environment:
Type: String
Default: dev
AllowedValues: [dev, staging, prod]
Description: Deployment environment
Resources:
# Parent stack that contains the nested stack
SNSStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: ./nested-templates/sns-stack.yaml
Parameters:
Environment: !Ref Environment
TopicName: !Sub "MyNotificationTopic-${Environment}"
Outputs:
SNSTopicARN:
Description: The ARN of the SNS topic
Value: !GetAtt SNSStack.Outputs.SNSTopicARN
Export:
Name: !Sub "${AWS::StackName}-SNSTopicARN"
Nested template:
AWSTemplateFormatVersion: '2010-09-09'
Description: Nested Stack for SNS Resources
Parameters:
Environment:
Type: String
Description: Deployment environment
TopicName:
Type: String
Description: Name for the SNS topic
Resources:
SNSTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub "${TopicName}"
DisplayName: !Sub "Notification Topic for ${Environment}"
Tags:
- Key: Environment
Value: !Ref Environment
Metadata:
SamResourceId: SNSTopic
Outputs:
SNSTopicARN:
Description: The ARN of the SNS topic
Value: !Ref SNSTopic
Export:
Name: !Sub "${AWS::StackName}-SNSTopicARN"
The Challenge: Importing an Existing SNS Topic
You have an existing SNS topic named "invoice" that you want to import into your CloudFormation stack. The topic already exists in your AWS account, and you want to manage it through your infrastructure-as-code.
Step 1: Update the Nested Template
First, add the SNS topic resource to your nested template:
SNSTopicInvoice:
Type: AWS::SNS::Topic
DeletionPolicy: Retain
Properties:
TopicName: invoice
Step 2: Create a Change Set for Import
Use the AWS CLI to create a change set for importing the existing resource. Replace the values of stack-name, template-body, resources to import, TopicArn, etc.
aws cloudformation create-change-set \
--stack-name test-SNSStack-1TN0405IE0OUB \
--change-set-name ImportSNSTopics \
--template-body file:///home/.../migration/nested-templates/sns-stack.yaml \
--change-set-type IMPORT \
--resources-to-import '[{
"ResourceType": "AWS::SNS::Topic",
"LogicalResourceId": "SNSTopicInvoice",
"ResourceIdentifier": {
"TopicArn": "arn:aws:sns:us-east-1:XXX:invoice"
}
}]' \
--parameters \
ParameterKey=Environment,ParameterValue=dev \
ParameterKey=TopicName,ParameterValue=MyNotificationTopic-dev
After creating the change set, execute it to perform the import:
aws cloudformation execute-change-set \
--stack-name test-SNSStack-1TN0405IE0OUB \
--change-set-name ImportSNSTopics \
--region us-east-1
The resource will be imported, and you can manage it from your IaC.
alejandro@minipc:~/Documents/delrioworks/migration$ aws cloudformation describe-stack-resources --stack-name test-SNSStack-1TN0405IE0OUB{"StackResources": [{"StackName": "test-SNSStack-1TN0405IE0OUB","StackId": "arn:aws:cloudformation:us-east-1:906310767457:stack/test-SNSStack-1TN0405IE0OUB/0de290e0-b42f-11f0-ac84-120e435c95d5","LogicalResourceId": "SNSTopic","PhysicalResourceId": "arn:aws:sns:us-east-1:906310767457:MyNotificationTopic-dev","ResourceType": "AWS::SNS::Topic","Timestamp": "2025-10-28T18:51:07.484000+00:00","ResourceStatus": "CREATE_COMPLETE","DriftInformation": {"StackResourceDriftStatus": "NOT_CHECKED"}}]}
No hay comentarios:
Publicar un comentario