How to send Amazon Simple Notification Service (SNS) messages to an Amazon Simple Queue Service (SQS) queue in a different AWS account
Table of contents
Prerequisites
- You should have an AWS account with access to both the SNS topic and SQS queue.
Configuration Steps
Configuring the SNS Topic
- Go to the AWS Management Console and navigate to the Amazon SNS service.
- Create a new SNS topic or select an existing topic that you want to use for sending messages to the SQS queue.
- Note down the ARN (Amazon Resource Name) of the SNS topic, as it will be needed in the following steps.
Configuring the SQS Queue in the Receiving Account
- Sign in to the AWS Management Console using the account that owns the SQS queue.
- Navigate to the Amazon SQS service.
- Create a new SQS queue or select an existing queue where you want to receive the messages.
- Note down the ARN of the SQS queue, as it will be used in the next step.
Granting Permissions to the SNS Topic
- Switch to the AWS account that owns the SNS topic.
- Navigate to the IAM (Identity and Access Management) service in the AWS Management Console.
- Create a new IAM policy or modify an existing policy to grant permissions to send messages to the SQS queue.
Here’s an example policy that allows sending messages to the SQS queue:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sqs:SendMessage", "Resource": "arn:aws:sqs:<region>:<account-id>:<queue-name>" } ] }Replace
<region>with the AWS region where the SQS queue is located,<account-id>with the AWS account ID that owns the SQS queue, and<queue-name>with the name of the SQS queue.- Attach the IAM policy to the IAM role or user that will be used to publish messages to the SNS topic.
Configuring the SNS Topic Subscription
- Switch back to the AWS account that owns the SNS topic.
- Navigate to the Amazon SNS service.
- Select the SNS topic that you want to send messages to the SQS queue.
- Click on the “Create subscription” button.
- Choose “Amazon SQS” as the protocol for the subscription.
- Enter the ARN of the SQS queue in the “Endpoint” field.
- Click on the “Create subscription” button to create the subscription.
Confirming the Subscription
- Switch to the AWS account that owns the SQS queue.
- Navigate to the Amazon SQS service.
- Select the SQS queue that should receive the messages.
- In the “Details” section, copy the “Queue ARN”.
- Switch back to the AWS account that owns the SNS topic.
- Navigate to the Amazon SNS service.
- Select the SNS topic that sends messages to the SQS queue.
- In the “Subscriptions” section, locate the subscription that corresponds to the SQS queue.
- Click on the subscription’s ARN to open the subscription details.
- Click on the “Request confirmation” button.
- Paste the SQS queue ARN and click on the “Confirm subscription” button.
Once the subscription is confirmed, you can start sending messages to the SNS topic