Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Subscribe an Amazon SQS queue to an Amazon SNS topic using an Amazon SDK
The following code examples show how to subscribe an Amazon SQS queue so it receives notifications from an Amazon SNS topic.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code examples:
- .NET
-
- Amazon SDK for .NET
-
Subscribe a queue to a topic with optional filters.
/// <summary>
/// Subscribe a queue to a topic with optional filters.
/// </summary>
/// <param name="topicArn">The ARN of the topic.</param>
/// <param name="useFifoTopic">The optional filtering policy for the subscription.</param>
/// <param name="queueArn">The ARN of the queue.</param>
/// <returns>The ARN of the new subscription.</returns>
public async Task<string> SubscribeTopicWithFilter(string topicArn, string? filterPolicy, string queueArn)
{
var subscribeRequest = new SubscribeRequest()
{
TopicArn = topicArn,
Protocol = "sqs",
Endpoint = queueArn
};
if (!string.IsNullOrEmpty(filterPolicy))
{
subscribeRequest.Attributes = new Dictionary<string, string> { { "FilterPolicy", filterPolicy } };
}
var subscribeResponse = await _amazonSNSClient.SubscribeAsync(subscribeRequest);
return subscribeResponse.SubscriptionArn;
}
- C++
-
- SDK for C++
-
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws::SNS::SNSClient snsClient(clientConfiguration);
Aws::SNS::Model::SubscribeRequest request;
request.SetTopicArn(topicARN);
request.SetProtocol("sqs");
request.SetEndpoint(queueARN);
Aws::SNS::Model::SubscribeOutcome outcome = snsClient.Subscribe(request);
if (outcome.IsSuccess()) {
Aws::String subscriptionARN = outcome.GetResult().GetSubscriptionArn();
std::cout << "The queue '" << queueName
<< "' has been subscribed to the topic '"
<< "'" << topicName << "'" << std::endl;
std::cout << "with the subscription ARN '" << subscriptionARN << "."
<< std::endl;
subscriptionARNS.push_back(subscriptionARN);
}
else {
std::cerr << "Error with TopicsAndQueues::Subscribe. "
<< outcome.GetError().GetMessage()
<< std::endl;
cleanUp(topicARN,
queueURLS,
subscriptionARNS,
snsClient,
sqsClient);
return false;
}
- JavaScript
-
- SDK for JavaScript (v3)
-
import { SubscribeCommand, SNSClient } from "@aws-sdk/client-sns";
const client = new SNSClient({});
export const subscribeQueue = async (
topicArn = "TOPIC_ARN",
queueArn = "QUEUE_ARN"
) => {
const command = new SubscribeCommand({
TopicArn: topicArn,
Protocol: "sqs",
Endpoint: queueArn,
});
const response = await client.send(command);
console.log(response);
// {
// '$metadata': {
// httpStatusCode: 200,
// requestId: '931e13d9-5e2b-543f-8781-4e9e494c5ff2',
// extendedRequestId: undefined,
// cfId: undefined,
// attempts: 1,
// totalRetryDelay: 0
// },
// SubscriptionArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:subscribe-queue-test-430895:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
// }
return response;
};
For a complete list of Amazon SDK developer guides and code examples, see
Using Amazon SNS with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.