Sub topic 12
Letting SNS write to SQS: queue access policies
This chapter assumes you can create an SQS queue and an SNS topic with boto3, and that you can read an IAM policy in JSON. You will learn why a new subscription delivers nothing until the queue allows it, and how to write a policy that trusts only one topic.
Why the subscription is not enough
An SNS topic sends messages to an SQS queue by calling sqs:SendMessage on it. SQS checks the queue's resource policy for each call. A new queue has no policy that lets SNS in, so SNS gets an access denied error and the messages never arrive. The subscription itself still looks healthy. This is the most common reason a first fan-out appears to do nothing.
The policy
The policy is attached to the queue. It names the SNS service as the principal and limits the grant to one topic with a condition on aws:SourceArn.
import json
import boto3
sqs = boto3.client("sqs")
sns = boto3.client("sns")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
queue_arn = sqs.get_queue_attributes(
QueueUrl=queue_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
topic_arn = sns.create_topic(Name="order-events")["TopicArn"]
# Allow only this topic to send messages to the queue
policy = {
"Version": "2012-10-17",
"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)}
)Each element has a job:
| Element | Purpose |
|---|---|
| `Principal` | Names the SNS service as the caller. |
| `Action` | Grants only `sqs:SendMessage`, not receive or delete. |
| `Resource` | Limits the grant to this one queue. |
| `Condition` | Accepts calls only when they come from this topic. |
Common errors
- No condition. Without
aws:SourceArn, any SNS topic in any account that knows the queue ARN could write to it. This is the confused deputy problem, where a trusted service is tricked into acting for the wrong party. - Wrong ARN in the condition. The topic ARN must match exactly, including the region. A typo gives silent denial.
- Overwriting the policy.
set_queue_attributesreplaces the wholePolicyattribute. If the queue already has statements, read them first and add yours, or you will remove existing access. - Several topics, one queue. Put each topic ARN in the
aws:SourceArnvalue as a list, or add one statement per topic. - Encrypted queues. If the queue uses a customer managed KMS key, the key policy must also let SNS use the key. The queue policy alone is not enough.
- Same account versus cross-account. In one account, the queue policy is enough. For a topic in another account, the queue policy must still name that topic's ARN.
Exercise
You subscribe a queue to a topic and publish a message. The subscription is confirmed, but the queue stays empty. What is the likely cause, and what is the narrowest fix?
Show answer
The queue has no resource policy that lets SNS send to it, so SNS gets access denied. Add a statement to the queue policy with Principal sns.amazonaws.com, Action sqs:SendMessage, Resource set to the queue ARN, and a Condition that matches aws:SourceArn to the topic ARN.