Message queuing service — pull consumers, long polling.
Pub/sub notifications — push to HTTP/S, Lambda, email, or SQS subscribers.
Unified topic/subscription model — supports push & pull subscriptions.
# Install AWS CLI (Ubuntu)
sudo apt update && sudo apt install awscli -y
# Configure credentials
aws configure
# Verify
aws sts get-caller-identity
// npm i @aws-sdk/client-sqs
const { SQSClient } = require("@aws-sdk/client-sqs");
const client = new SQSClient({ region: 'ap-south-1' });
Python
import boto3
sqs = boto3.client('sqs', region_name='ap-south-1')
# Install/Configure same as AWS CLI
aws --version
aws configure
# SNS quick verify
aws sns list-topics
// npm i @aws-sdk/client-sns
const { SNSClient } = require('@aws-sdk/client-sns');
const sns = new SNSClient({ region: 'ap-south-1' });
Python
import boto3
sns = boto3.client('sns', region_name='ap-south-1')
# Install gcloud SDK
# Ubuntu: follow Google Cloud SDK install
# Auth & project
gcloud auth login
gcloud config set project YOUR_PROJECT_ID
# Pub/Sub check
gcloud pubsub topics list
// npm i @google-cloud/pubsub
const { PubSub } = require('@google-cloud/pubsub');
const pubsub = new PubSub({ projectId: 'YOUR_PROJECT_ID' });
Python
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
# Create queue (standard)
aws sqs create-queue --queue-name my-queue
# Get URL from response (QueueUrl)
const { CreateQueueCommand } = require('@aws-sdk/client-sqs');
await client.send(new CreateQueueCommand({ QueueName: 'my-queue' }));
response = sqs.create_queue(QueueName='my-queue')
queue_url = response['QueueUrl']
# Create SNS topic
aws sns create-topic --name my-topic
# Response contains TopicArn
const { CreateTopicCommand } = require('@aws-sdk/client-sns');
await sns.send(new CreateTopicCommand({ Name: 'my-topic' }));
response = sns.create_topic(Name='my-topic')
topic_arn = response['TopicArn']
# Create topic
gcloud pubsub topics create my-topic
const [topic] = await pubsub.createTopic('my-topic');
from google.cloud import pubsub_v1
publisher.create_topic(request={"name": publisher.topic_path("YOUR_PROJECT_ID", "my-topic")})
# Send message to SQS
aws sqs send-message --queue-url QUEUE_URL --message-body "hello from cli"
const { SendMessageCommand } = require('@aws-sdk/client-sqs');
await client.send(new SendMessageCommand({ QueueUrl: QUEUE_URL, MessageBody: 'hello' }));
sqs.send_message(QueueUrl=QUEUE_URL, MessageBody='hello')
# Publish to SNS topic
aws sns publish --topic-arn TOPIC_ARN --message "hi subscribers"
const { PublishCommand } = require('@aws-sdk/client-sns');
await sns.send(new PublishCommand({ TopicArn: TOPIC_ARN, Message: 'hi subscribers' }));
sns.publish(TopicArn=TOPIC_ARN, Message='hi subscribers')
# Publish to topic
gcloud pubsub topics publish my-topic --message "hello pubsub"
const dataBuffer = Buffer.from('hello pubsub');
await pubsub.topic('my-topic').publish(dataBuffer);
future = publisher.publish(publisher.topic_path('YOUR_PROJECT_ID','my-topic'), b'hello pubsub')
message_id = future.result()
# Pull messages (long polling)
aws sqs receive-message --queue-url QUEUE_URL --max-number-of-messages 10 --wait-time-seconds 10
# Delete after processing:
aws sqs delete-message --queue-url QUEUE_URL --receipt-handle RECEIPT_HANDLE
const { ReceiveMessageCommand, DeleteMessageCommand } = require('@aws-sdk/client-sqs');
const r = await client.send(new ReceiveMessageCommand({ QueueUrl: QUEUE_URL, MaxNumberOfMessages: 5 }));
// process r.Messages then delete each via DeleteMessageCommand
resp = sqs.receive_message(QueueUrl=QUEUE_URL, MaxNumberOfMessages=10, WaitTimeSeconds=10)
# then: sqs.delete_message(QueueUrl=QUEUE_URL, ReceiptHandle='...')
# Subscribe an email endpoint
aws sns subscribe --topic-arn TOPIC_ARN --protocol email --notification-endpoint you@example.com
# Subscribe SQS queue to topic
aws sns subscribe --topic-arn TOPIC_ARN --protocol sqs --notification-endpoint SQS_QUEUE_ARN
// Node.js
await sns.send(new SubscribeCommand({ TopicArn: TOPIC_ARN, Protocol: 'email', Endpoint: 'you@example.com' }));
# For SQS subscribe use Protocol 'sqs' and pass queue ARN
# Create subscription & pull messages
gcloud pubsub subscriptions create my-sub --topic=my-topic
gcloud pubsub subscriptions pull my-sub --auto-ack --limit=10
const subscription = pubsub.subscription('my-sub');
const [messages] = await subscription.pull({ maxMessages: 5 });
// ack messages via message.ack()
def callback(message):
print(message.data)
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
# streaming_pull_future.result() # blocks
# List queues
aws sqs list-queues
# Delete queue (DESTRUCTIVE) - will remove queue and messages
aws sqs delete-queue --queue-url QUEUE_URL
// Node.js
await client.send(new DeleteQueueCommand({ QueueUrl: QUEUE_URL }));
# Python
sqs.delete_queue(QueueUrl=QUEUE_URL)
# List topics
aws sns list-topics
# Delete topic (DESTRUCTIVE)
aws sns delete-topic --topic-arn TOPIC_ARN
// Node.js
await sns.send(new DeleteTopicCommand({ TopicArn: TOPIC_ARN }));
# Python
sns.delete_topic(TopicArn=TOPIC_ARN)
# List topics
gcloud pubsub topics list
# Delete topic/subscription (DESTRUCTIVE)
gcloud pubsub topics delete my-topic
gcloud pubsub subscriptions delete my-sub
# Node.js
await pubsub.topic('my-topic').delete();
await pubsub.subscription('my-sub').delete();
# Python
publisher.delete_topic(request={"topic": publisher.topic_path('YOUR_PROJECT_ID','my-topic')})
subscriber.delete_subscription(request={"subscription": subscriber.subscription_path('YOUR_PROJECT_ID','my-sub')})