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?

Resolution

Choose one of the following solutions based on your use case:

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.


Did this article help you?

Anything we could improve?


Need more help?