How to Use AWS SNS with SQS for Message Delivery
To use
AWS SNS with SQS, create an SNS topic and an SQS queue, then subscribe the queue to the topic. When messages are published to the SNS topic, they are automatically delivered to the subscribed SQS queue for processing.Syntax
To connect SNS with SQS, you need to create an SNS topic, create an SQS queue, and then subscribe the queue to the topic. The subscription requires the queue's ARN (Amazon Resource Name) and the topic's ARN.
- SNS Topic: The source that sends messages.
- SQS Queue: The destination that receives messages.
- Subscription: Links the SNS topic to the SQS queue.
bash
aws sns create-topic --name MyTopic aws sqs create-queue --queue-name MyQueue aws sns subscribe --topic-arn arn:aws:sns:region:account-id:MyTopic --protocol sqs --notification-endpoint arn:aws:sqs:region:account-id:MyQueue
Example
This example shows how to create an SNS topic, an SQS queue, subscribe the queue to the topic, and then publish a message to the topic. The message will be delivered to the SQS queue.
bash
aws sns create-topic --name ExampleTopic aws sqs create-queue --queue-name ExampleQueue # Get ARNs TOPIC_ARN=$(aws sns list-topics --query 'Topics[?contains(TopicArn, `ExampleTopic`)].TopicArn' --output text) QUEUE_URL=$(aws sqs get-queue-url --queue-name ExampleQueue --output text) QUEUE_ARN=$(aws sqs get-queue-attributes --queue-url $QUEUE_URL --attribute-names QueueArn --query 'Attributes.QueueArn' --output text) # Subscribe SQS queue to SNS topic aws sns subscribe --topic-arn $TOPIC_ARN --protocol sqs --notification-endpoint $QUEUE_ARN # Allow SNS to send messages to SQS queue aws sqs set-queue-attributes --queue-url $QUEUE_URL --attributes '{"Policy":"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"'$QUEUE_ARN'\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"'$TOPIC_ARN'\"}}}]}"}' # Publish a message aws sns publish --topic-arn $TOPIC_ARN --message "Hello from SNS to SQS!" # Receive the message from SQS aws sqs receive-message --queue-url $QUEUE_URL --max-number-of-messages 1
Output
{
"Messages": [
{
"MessageId": "12345678-1234-1234-1234-123456789012",
"ReceiptHandle": "AQEB...",
"MD5OfBody": "9bb58f26192e4ba00f01e2e7b136bbd8",
"Body": "Hello from SNS to SQS!"
}
]
}
Common Pitfalls
Common mistakes when using SNS with SQS include:
- Not setting the correct queue policy to allow SNS to send messages to the SQS queue.
- Using the wrong ARN for subscription or permissions.
- Forgetting to subscribe the queue to the topic.
- Not handling message deletion from the queue after processing, causing repeated delivery.
Always verify the queue policy and subscription status.
bash
## Wrong: Missing queue policy allowing SNS
aws sns subscribe --topic-arn arn:aws:sns:region:account-id:MyTopic --protocol sqs --notification-endpoint arn:aws:sqs:region:account-id:MyQueue
# This will fail to deliver messages because SQS denies SNS permission.
## Right: Add queue policy to allow SNS
aws sqs set-queue-attributes --queue-url https://sqs.region.amazonaws.com/account-id/MyQueue --attributes '{"Policy":"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"arn:aws:sqs:region:account-id:MyQueue\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:region:account-id:MyTopic\"}}}]}"}'Quick Reference
- Create SNS Topic:
aws sns create-topic --name MyTopic - Create SQS Queue:
aws sqs create-queue --queue-name MyQueue - Subscribe Queue to Topic:
aws sns subscribe --topic-arn <topic-arn> --protocol sqs --notification-endpoint <queue-arn> - Set Queue Policy: Allow SNS to send messages to SQS queue.
- Publish Message:
aws sns publish --topic-arn <topic-arn> --message "Your message"
Key Takeaways
Subscribe your SQS queue to the SNS topic using the queue ARN and topic ARN.
Set the correct SQS queue policy to allow SNS to send messages to the queue.
Publish messages to the SNS topic to deliver them automatically to the subscribed SQS queue.
Always delete messages from the SQS queue after processing to avoid duplicates.
Verify ARNs and permissions carefully to ensure smooth message delivery.