How do I create a subscription between my Amazon SQS queue and an Amazon SNS topic in AWS CloudFormation?
Last updated: 2019-05-09
How do I create a subscription between my Amazon Simple Queue Service (Amazon SQS) queue and Amazon Simple Notification Service (Amazon SNS) topic in AWS CloudFormation?
Choose one of the following solutions based on your use case:
- If the SNS topic and SQS queue are in the same stack, use an AWS CloudFormation template to create a topic that sends messages to SQS queues.
- If the SNS topic is in one stack and the SQS queue that will subscribe to that SNS topic is in another stack in the same AWS Region, create a cross-stack reference.
Note: When you create a cross-stack reference, you export the Amazon Resource Name (ARN) of the SQS queue in one stack, and then import that SQS queue ARN in the subscription endpoint property of the SNS topic in the other stack.
- If the SNS topic and SQS queue are in separate AWS Regions, follow the steps in the Use the AWS::SNS::Subscription resource to set up a cross-region subscription section.
Use the AWS::SNS::Subscription resource to set up a cross-region subscription
1. In the AWS CloudFormation template for the stack in one AWS Region, declare the SNS topic in that region, and then create an output resource to annotate the SNS topic ARN. See the following example:
Resources: SnsTopic: Type: AWS::SNS::Topic Outputs: SnsTopicArn: Value: !Ref SnsTopic
2. In an AWS CloudFormation template for the other stack in the other AWS Region, define the AWS::SNS::Subscription resource and the SQS queue. Replace awsSNSTopicArnExample with your SNS topic ARN, and replace us-east-1 with your AWS Region. See the following example:
Parameters: SNSTopicARN: Type: String Description: awsSNSTopicArnExample TopicRegion: Type: String Description: us-east-1 Resources: Queue: Type: AWS::SQS::Queue SnsSubscription: Type: AWS::SNS::Subscription Properties: Protocol: sqs Endpoint: !GetAtt Queue.Arn Region: !Ref TopicRegion TopicArn: !Ref SNSTopicARN
Your cross-region subscription is now set up.