You can connect an Amazon Simple Queue Service (SQS) queue to an Amazon Simple Notification Service (SNS) topic by subscribing the SQS queue to the SNS topic. This allows messages published to the SNS topic to be delivered to the SQS queue.
Here’s a general step-by-step process to do this:
🛠️ Step-by-Step: Connect SQS Queue to SNS Topic
1. Create SNS Topic and SQS Queue
If you haven't already:
- Create an SNS Topic.
- Create an SQS Queue.
💡 SNS and SQS should ideally be in the same AWS Region for ease of integration.
2. Get the SQS Queue ARN
You'll need the Amazon Resource Name (ARN) of the SQS queue for the subscription.
- Go to SQS Console > Your Queue > Details tab.
- Copy the Queue ARN (e.g.,
arn:aws:sqs:us-east-1:123456789012:MyQueue).
3. Set the Correct Permissions on the SQS Queue
SNS needs permission to send messages to the SQS queue. You must add the proper policy to the SQS Access Policy.
Example Policy:
{
"Version": "2012-10-17",
"Id": "QueuePolicy",
"Statement": [
{
"Sid": "Allow-SNS-SendMessage",
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:sns:us-east-1:123456789012:MySNSTopic"
}
}
}
]
}
Replace:
- Topic and Queue ARNs
- Region and Account ID
Add or merge this into the Access Policy of the SQS queue via the SQS Console or SDK.
4. Subscribe the SQS Queue to the SNS Topic
You can do this via Console, AWS CLI, or SDK.
✅ Console:
- Go to SNS Console > Topics > Your Topic > Create Subscription
- Protocol:
Amazon SQS
- Endpoint: Choose the SQS queue ARN
- Click Create Subscription
✅ AWS CLI:
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic \
--protocol sqs \
--notification-endpoint arn:aws:sqs:us-east-1:123456789012:MyQueue
5. Test the Integration
- Publish a test message to the SNS topic
- Check that it’s delivered to the SQS queue
Example: Via AWS CLI
aws sns publish \
--topic-arn arn:aws:sns:us-east-1:123456789012:MySNSTopic \
--message "Hello from SNS!"
Then check your SQS queue for the message.
🔁 Summary
| Step | Action |
|---|
| 1 | Create SNS Topic and SQS Queue |
| 2 | Get SQS Queue ARN |
| 3 | Add permission to SQS to allow SNS to send messages |
| 4 | Subscribe SQS to SNS |
| 5 | Test publishing a message |
Let me know if you want help with Terraform, CloudFormation, or CDK to automate this!