Amazon SQS & SNS Messaging
Queue messages with SQS and publish-subscribe with SNS using boto3 and AWS SDK for JavaScript v3.
Busca en todas las páginas de la documentación
Queue messages with SQS and publish-subscribe with SNS using boto3 and AWS SDK for JavaScript v3.
Create a new queue to hold messages for asynchronous processing.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
response = sqs.create_queue(QueueName="my-queue")
queue_url = response["QueueUrl"]// --- TypeScript (AWS SDK v3) ---
import { SQSClient, CreateQueueCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
const { QueueUrl } = await sqs.send(
new CreateQueueCommand({ QueueName: "my-queue" })
);Retrieve the URL for an existing queue by name.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
response = sqs.get_queue_url(QueueName="my-queue")
queue_url = response["QueueUrl"]// --- TypeScript (AWS SDK v3) ---
import { SQSClient, GetQueueUrlCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
const { QueueUrl } = await sqs.send(
new GetQueueUrlCommand({ QueueName: "my-queue" })
);Enqueue a single message to a queue by URL.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
sqs.send_message(QueueUrl=queue_url, MessageBody="hello")// --- TypeScript (AWS SDK v3) ---
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(
new SendMessageCommand({ QueueUrl: queue_url, MessageBody: "hello" })
);Send multiple messages in a single request for efficiency.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
sqs.send_message_batch(
QueueUrl=queue_url,
Entries=[
{"Id": "1", "MessageBody": "msg1"},
{"Id": "2", "MessageBody": "msg2"},
]
)// --- TypeScript (AWS SDK v3) ---
import { SQSClient, SendMessageBatchCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(
new SendMessageBatchCommand({
QueueUrl: queue_url,
Entries: [
{ Id: "1", MessageBody: "msg1" },
{ Id: "2", MessageBody: "msg2" },
],
})
);Receive messages with long polling to reduce empty receives and latency.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
response = sqs.receive_message(
QueueUrl=queue_url, WaitTimeSeconds=20, MaxNumberOfMessages=10
)
messages = response.get("Messages", [])// --- TypeScript (AWS SDK v3) ---
import { SQSClient, ReceiveMessageCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
const { Messages } = await sqs.send(
new ReceiveMessageCommand({
QueueUrl: queue_url,
WaitTimeSeconds: 20,
MaxNumberOfMessages: 10,
})
);Remove a message from the queue after processing via its receipt handle.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=receipt_handle)// --- TypeScript (AWS SDK v3) ---
import { SQSClient, DeleteMessageCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(
new DeleteMessageCommand({ QueueUrl: queue_url, ReceiptHandle: receipt_handle })
);Extend or reduce the visibility timeout for in-flight messages.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
sqs.change_message_visibility(
QueueUrl=queue_url, ReceiptHandle=receipt_handle, VisibilityTimeout=60
)// --- TypeScript (AWS SDK v3) ---
import { SQSClient, ChangeMessageVisibilityCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(
new ChangeMessageVisibilityCommand({
QueueUrl: queue_url,
ReceiptHandle: receipt_handle,
VisibilityTimeout: 60,
})
);Send a message to a FIFO queue with ordering and optional deduplication.
# --- Python (boto3) ---
import boto3
sqs = boto3.client("sqs")
sqs.send_message(
QueueUrl=fifo_queue_url,
MessageBody="order-123",
MessageGroupId="orders",
MessageDeduplicationId="dedup-1",
)// --- TypeScript (AWS SDK v3) ---
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(
new SendMessageCommand({
QueueUrl: fifo_queue_url,
MessageBody: "order-123",
MessageGroupId: "orders",
MessageDeduplicationId: "dedup-1",
})
);Create a topic for publish-subscribe messaging.
# --- Python (boto3) ---
import boto3
sns = boto3.client("sns")
response = sns.create_topic(Name="my-topic")
topic_arn = response["TopicArn"]// --- TypeScript (AWS SDK v3) ---
import { SNSClient, CreateTopicCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
const { TopicArn } = await sns.send(
new CreateTopicCommand({ Name: "my-topic" })
);Send a message to a topic, which routes to all subscribers.
# --- Python (boto3) ---
import boto3
sns = boto3.client("sns")
response = sns.publish(TopicArn=topic_arn, Message="hello subscribers")
message_id = response["MessageId"]// --- TypeScript (AWS SDK v3) ---
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
const { MessageId } = await sns.send(
new PublishCommand({ TopicArn: topic_arn, Message: "hello subscribers" })
);Subscribe a queue, email, or Lambda to receive messages from a topic.
# --- Python (boto3) ---
import boto3
sns = boto3.client("sns")
response = sns.subscribe(
TopicArn=topic_arn, Protocol="sqs", Endpoint=queue_arn
)
subscription_arn = response["SubscriptionArn"]// --- TypeScript (AWS SDK v3) ---
import { SNSClient, SubscribeCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
const { SubscriptionArn } = await sns.send(
new SubscribeCommand({
TopicArn: topic_arn,
Protocol: "sqs",
Endpoint: queue_arn,
})
);Subscribe an SQS queue to an SNS topic, then attach a queue policy to allow SNS to send messages.
# --- Python (boto3) ---
import boto3
import json
sns = boto3.client("sns")
sqs = boto3.client("sqs")
sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=queue_arn)
policy = {
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "sns.amazonaws.com"},
"Action": "sqs:SendMessage",
"Resource": queue_arn,
"Condition": {"ArnEquals": {"aws:SourceArn": topic_arn}},
}
]
}
sqs.set_queue_attributes(
QueueUrl=queue_url, Attributes={"Policy": json.dumps(policy)}
)// --- TypeScript (AWS SDK v3) ---
import { SNSClient, SubscribeCommand } from "@aws-sdk/client-sns";
import { SQSClient, SetQueueAttributesCommand } from "@aws-sdk/client-sqs";
const sns = new SNSClient({});
const sqs = new SQSClient({});
await sns.send(
new SubscribeCommand({
TopicArn: topic_arn,
Protocol: "sqs",
Endpoint: queue_arn,
})
);
const policy = {
Statement: [
{
Effect: "Allow",
Principal: { Service: "sns.amazonaws.com" },
Action: "sqs:SendMessage",
Resource: queue_arn,
Condition: { ArnEquals: { "aws:SourceArn": topic_arn } },
},
],
};
await sqs.send(
new SetQueueAttributesCommand({
QueueUrl: queue_url,
Attributes: { Policy: JSON.stringify(policy) },
})
);Send a message with metadata attributes for filtering subscriptions.
# --- Python (boto3) ---
import boto3
sns = boto3.client("sns")
sns.publish(
TopicArn=topic_arn,
Message="order notification",
MessageAttributes={
"priority": {"DataType": "String", "StringValue": "high"},
"region": {"DataType": "String", "StringValue": "us-west-2"},
},
)// --- TypeScript (AWS SDK v3) ---
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
await sns.send(
new PublishCommand({
TopicArn: topic_arn,
Message: "order notification",
MessageAttributes: {
priority: { DataType: "String", StringValue: "high" },
region: { DataType: "String", StringValue: "us-west-2" },
},
})
);Configure an SQS queue to send messages that fail processing multiple times to a DLQ.
# --- Python (boto3) ---
import boto3
import json
sqs = boto3.client("sqs")
redrive_policy = {
"deadLetterTargetArn": dlq_arn,
"maxReceiveCount": 3,
}
sqs.set_queue_attributes(
QueueUrl=queue_url,
Attributes={"RedrivePolicy": json.dumps(redrive_policy)},
)// --- TypeScript (AWS SDK v3) ---
import { SQSClient, SetQueueAttributesCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
const redrivePol = {
deadLetterTargetArn: dlq_arn,
maxReceiveCount: 3,
};
await sqs.send(
new SetQueueAttributesCommand({
QueueUrl: queue_url,
Attributes: { RedrivePolicy: JSON.stringify(redrivePol) },
})
);Send an SMS message directly via SNS without a topic.
# --- Python (boto3) ---
import boto3
sns = boto3.client("sns")
response = sns.publish(PhoneNumber="+1234567890", Message="hello")
message_id = response["MessageId"]// --- TypeScript (AWS SDK v3) ---
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
const { MessageId } = await sns.send(
new PublishCommand({ PhoneNumber: "+1234567890", Message: "hello" })
);Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última actualización: 23 jul 2026