Hands-On: Send & Receive Messages
Create a queue and run the full send → receive → delete loop.
1 min read
Create a queue with long polling enabled:
Terminal
aws sqs create-queue --queue-name notesnap-jobs --attributes ReceiveMessageWaitTimeSeconds=20,VisibilityTimeout=60Then produce and consume from Python:
Try it
sqs_demo.py
import json
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="notesnap-jobs")["QueueUrl"]
# Producer: send a job
sqs.send_message(QueueUrl=queue_url, MessageBody=json.dumps({"noteKey": "notes/todo.txt"}))
# Consumer: receive up to 10 messages, waiting up to 20s for them to arrive
resp = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=10, WaitTimeSeconds=20)
for msg in resp.get("Messages", []):
job = json.loads(msg["Body"])
print("Processing", job["noteKey"])
# ... do the work ...
sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=msg["ReceiptHandle"])
print("Deleted", msg["MessageId"])Note
You delete using the receipt handle from the receive call, not the message ID. Each receive gives a new receipt handle.
Key takeaways
- send_message → receive_message → delete_message is the whole loop.
- Use WaitTimeSeconds for long polling.
- Delete with the ReceiptHandle from the receive.
Check your understanding
Pick an answer to see the explanation.
1. What do you pass to delete_message?