Sub topic 2
Amazon SQS queues: what they are and how to create one
This chapter assumes you have an AWS account with credentials configured, know how regions and IAM permissions work, can read JSON, and can run Python with boto3 installed.
What a queue is
Amazon SQS (Simple Queue Service) is a managed message queue. A producer sends messages to the queue. SQS stores them redundantly across multiple Availability Zones. A consumer later pulls them out and processes them. The producer does not wait for the consumer, and the two do not need to be running at the same time.
A queue is a buffer. If consumers slow down or fail, messages wait in the queue instead of being lost or pushing back on the producer.
Two identifiers
Every queue has two identifiers, and they are used in different places.
| Identifier | Looks like | Used for |
|---|---|---|
| Queue URL | `https://sqs.us-east-1.amazonaws.com/123456789012/orders` | API calls such as send, receive, and delete |
| Queue ARN | `arn:aws:sqs:us-east-1:123456789012:orders` | Policies, redrive settings, and SNS subscriptions |
Passing a URL where an ARN is expected, or the reverse, is a common mistake.
Queue settings
You set queue behavior through attributes. These four matter first:
VisibilityTimeout: how long a received message stays hidden from other consumers. The default is 30 seconds.ReceiveMessageWaitTimeSeconds: a value above 0 turns on long polling by default.MessageRetentionPeriod: how long unconsumed messages are kept. The default is 4 days and the maximum is 14.RedrivePolicy: where messages go after too many failed receives.
Later chapters cover each of these in detail. For now, you only need to see where they are configured.
Creating a queue with a dead-letter queue
The script below creates the dead-letter queue first, because the main queue's redrive policy needs its ARN. It then creates the main queue.
import json
import boto3
sqs = boto3.client("sqs")
# Dead-letter queue for messages that keep failing
dlq_url = sqs.create_queue(QueueName="orders-dlq")["QueueUrl"]
dlq_arn = sqs.get_queue_attributes(
QueueUrl=dlq_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
# Main queue: 60 s visibility timeout, long polling by default,
# and move a message to the DLQ after 5 failed receives
queue_url = sqs.create_queue(
QueueName="orders",
Attributes={
"VisibilityTimeout": "60",
"ReceiveMessageWaitTimeSeconds": "20",
"RedrivePolicy": json.dumps(
{"deadLetterTargetArn": dlq_arn, "maxReceiveCount": "5"}
),
},
)["QueueUrl"]
print(queue_url)Common errors
- Non-string attribute values. The API requires every attribute value to be a string, so write
"60", not60. EvenmaxReceiveCountis a string inside the JSON. - Name conflicts. A queue name must be unique within an account and region. Names may use letters, digits, hyphens, and underscores, up to 80 characters. Calling
create_queueagain with the same name and the same attributes returns the existing queue. Calling it with different attributes raisesQueueNameExists. - Mismatched dead-letter queues. The dead-letter queue must be the same type as the source queue (standard or FIFO). It must also be in the same account and region.
- Wrong region. A queue exists in one region. A client in another region cannot find it, and the error looks like the queue does not exist.
Exercise
Why does the script create orders-dlq before orders, and which identifier of the dead-letter queue does the redrive policy use?
Show answer
The redrive policy of orders must contain the dead-letter queue's ARN (deadLetterTargetArn), so the dead-letter queue has to exist first. The script reads the ARN with get_queue_attributes. The queue URL is not accepted there.