Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

SNS to SQS

Created with SnapSNSSQS

Create an SQS queue that subscribes to an SNS topic.

The SAM template deploys a SNS topic and an SQS queue. The SQS queue is subscribed to the SNS topic. SNS invokes the SQS queue when new messages are available.
When messages are sent to the SNS topic, they are delivered as a JSON event payload to the SQS queue.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless patterns - SNS to SQS (uksb-1tthgi812) (tag:sns-sqs)

Resources:
  # Define the SQS queue
  MySqsQueue:
    Type: AWS::SQS::Queue

  # Define the SNS topic
  MySnsTopic:
    Type: AWS::SNS::Topic
    Properties:
      # Subscribes the SQS queue to the SNS topic
      Subscription:
        - Protocol: sqs
          Endpoint: !GetAtt MySqsQueue.Arn

  # Policy allows SNS to publish to this SQS queue
  SnsToSqsPolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Sid: "Allow SNS publish to SQS"
            Effect: Allow
            Principal: 
              Service: "sns.amazonaws.com"
            Resource: !GetAtt MySqsQueue.Arn
            Action: SQS:SendMessage
            Condition:
              ArnEquals:
                aws:SourceArn: !Ref MySnsTopic
      Queues:
        - Ref: MySqsQueue

Outputs:
  MySqsQueueName:
    Description: SQS queue name
    Value: !GetAtt MySqsQueue.QueueName
  MySqsQueueArn:
    Description: SQS queue ARN
    Value: !GetAtt MySqsQueue.Arn
  MySqsQueueURL:
    Description: SQS queue URL
    Value: !Ref MySqsQueue    
  MySnsTopicName:
    Description: SNS topic name
    Value: !GetAtt MySnsTopic.TopicName
  MySnsTopicArn:
    Description: SNS topic ARN
    Value: !Ref MySnsTopic

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/sns-sqs

Deploy

sam deploy --guided


Testing

Use the AWS CLI to send a message to the SNS topic and observe the event delivered to the Lambda function:
1. Send the SNS message: aws sns publish --topic-arn ENTER_SNS_TOPIC_ARN --subject testSubject --message testMessage.
2. Retrieve the message from the SQS queue, using the queue URL from the AWS SAM deployment outputs: aws sqs receive-message --queue-url ENTER_YOUR_QUEUE_URL.

Cleanup

1. Delete the stack: sam delete --stack-name STACK_NAME.
2. Confirm the stack has been deleted: aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

James Beswick

Presented by James Beswick

I am a self-confessed serverless geek and enjoy helping developers build faster with serverless! I have been software developer and product manager before becoming a Developer Advocate. Ask me anything!

Follow on LinkedIn