Sub topic 17
Encrypting queues and topics
This chapter assumes you can create SQS queues and SNS topics with boto3, read IAM and KMS policy JSON, and know what a CloudWatch alarm is.
Encryption in transit and at rest
Every SQS and SNS call goes over HTTPS, so data is encrypted in transit. To refuse plain HTTP, add a Deny statement to the queue or topic policy with the condition "Bool": {"aws:SecureTransport": "false"}.
At rest, you have two choices for SQS.
| Option | Key owner | Notes |
|---|---|---|
| SSE-SQS | SQS | New queues have it on by default. It has no extra cost and no key to manage. |
| SSE-KMS | You, in AWS KMS | You control the key policy and can audit key use in CloudTrail. KMS requests are billed. |
SNS topics use SSE-KMS only, and you turn it on by setting a key on the topic.
import boto3
sqs = boto3.client("sqs")
sns = boto3.client("sns")
key_id = "alias/orders-key" # a customer managed KMS key
queue_url = sqs.create_queue(
QueueName="orders-secure",
Attributes={
"KmsMasterKeyId": key_id,
"KmsDataKeyReusePeriodSeconds": "300", # reuse data keys to cut KMS calls
},
)["QueueUrl"]
topic_arn = sns.create_topic(
Name="order-events-secure",
Attributes={"KmsMasterKeyId": key_id},
)["TopicArn"]
print(queue_url, topic_arn)Common errors
- Producers and consumers get
AccessDeniedfrom KMS. With SSE-KMS, senders needkms:GenerateDataKeyandkms:Decrypt. Receivers needkms:Decrypt. - SNS publishes to an encrypted queue, and nothing arrives. The SNS service principal (
sns.amazonaws.com) must be allowedkms:GenerateDataKeyandkms:Decryptin the key policy. - You used the AWS managed key
alias/aws/sqsoralias/aws/sns. You cannot edit its policy, so other services cannot use it. Use a customer managed key for fan-out.
Monitoring queue depth, message age, and dead-letter queues
SQS publishes metrics to CloudWatch in the AWS/SQS namespace, with the QueueName dimension.
ApproximateNumberOfMessagesVisibleis the queue depth, the messages waiting to be received.ApproximateNumberOfMessagesNotVisiblecounts messages in flight, hidden by the visibility timeout.ApproximateAgeOfOldestMessageis the age in seconds of the oldest waiting message. It shows lag better than depth does, because a deep queue that drains quickly is healthy.
Any message in a dead-letter queue means something failed five times, or whatever your maxReceiveCount is. Alarm on a DLQ depth above zero:
import boto3
cw = boto3.client("cloudwatch")
sns = boto3.client("sns")
alert_topic = sns.create_topic(Name="ops-alerts")["TopicArn"]
cw.put_metric_alarm(
AlarmName="orders-dlq-not-empty",
Namespace="AWS/SQS",
MetricName="ApproximateNumberOfMessagesVisible",
Dimensions=[{"Name": "QueueName", "Value": "orders-dlq"}],
Statistic="Maximum",
Period=60,
EvaluationPeriods=1,
Threshold=0,
ComparisonOperator="GreaterThanThreshold",
TreatMissingData="notBreaching",
AlarmActions=[alert_topic],
)For the main queue, alarm on ApproximateAgeOfOldestMessage above the delay your application can tolerate.
Exercise
A standard queue is encrypted with SSE-KMS using a customer managed key. An SNS topic publishes to it, the subscription is confirmed, and the queue policy trusts the topic. No messages arrive. What do you check first, and which metric alarm would have warned you that something was wrong?
Show answer
Check the KMS key policy. It must allow sns.amazonaws.com to call kms:GenerateDataKey and kms:Decrypt. Without that, SNS cannot write to the encrypted queue. A CloudWatch alarm on the subscription's delivery failures (NumberOfNotificationsFailed on the topic) would flag it. If a DLQ is attached to the subscription, an alarm on its ApproximateNumberOfMessagesVisible above zero would also show it.