Sub topic 11
Subscribing endpoints: SQS, Lambda, HTTP/S, email, and more
This chapter assumes you can publish to an SNS topic and have boto3 configured with permissions for SNS, SQS, and Lambda. A subscription ties one endpoint to a topic. Each message published to the topic is then delivered to every confirmed subscription whose filter policy matches it.
Supported protocols
| Protocol | Endpoint value | Confirmation |
|---|---|---|
| `sqs` | Queue ARN | Automatic, if the queue policy allows the topic |
| `lambda` | Function ARN | Automatic, if the function's resource policy allows SNS |
| `http`, `https` | URL | Your endpoint must confirm |
| `email`, `email-json` | Email address | The recipient must click a link |
| `sms` | Phone number | None |
| `firehose` | Delivery stream ARN | Automatic |
| `application` | Mobile push endpoint ARN | None |
Use sqs and lambda for service-to-service work. Use email, sms, and mobile push to notify people. Use https to reach systems outside AWS.
Subscribing in code
The queue subscription appears in the fan-out chapter, so this example covers the other three.
import boto3
sns = boto3.client("sns")
lambda_client = boto3.client("lambda")
topic_arn = sns.create_topic(Name="order-events")["TopicArn"]
function_arn = "arn:aws:lambda:us-east-1:123456789012:function:notify-orders"
# Lambda: SNS must be allowed to invoke the function
lambda_client.add_permission(
FunctionName="notify-orders",
StatementId="sns-invoke",
Action="lambda:InvokeFunction",
Principal="sns.amazonaws.com",
SourceArn=topic_arn,
)
sns.subscribe(TopicArn=topic_arn, Protocol="lambda", Endpoint=function_arn)
# HTTPS: SNS posts a confirmation request to this URL
sns.subscribe(
TopicArn=topic_arn,
Protocol="https",
Endpoint="https://example.com/sns-hook",
)
# Email: the address receives a confirmation message
sns.subscribe(
TopicArn=topic_arn,
Protocol="email",
Endpoint="[email protected]",
)Replace the account ID, function name, URL, and address with your own values. The SourceArn condition in add_permission stops other topics from invoking the function.
Confirmation
SNS sends nothing to an endpoint until its subscription is confirmed. Until then the subscription ARN reads PendingConfirmation.
- Email: the recipient clicks the link in the confirmation message.
- HTTP/S: SNS posts a JSON body with
Typeset toSubscriptionConfirmation, aToken, and aSubscribeURL. Your handler must fetch that URL or callsns.confirm_subscription(TopicArn=..., Token=...). The token is valid for three days. - SQS and Lambda in your own account:
subscribeconfirms them at once. Messages still fail if the access policy is missing, so check the policy first when nothing arrives.
Managing subscriptions
list_subscriptions_by_topic shows each subscription and its state. unsubscribe takes the subscription ARN. Email links include an unsubscribe option. A subscription can also carry its own FilterPolicy, RawMessageDelivery, and dead-letter queue settings, which later chapters cover.
Exercise
You subscribed a Lambda function to a topic with sns.subscribe, and the call succeeded. Published messages never invoke the function. What is the most likely cause, and how do you fix it?
Show answer
The function's resource-based policy does not allow sns.amazonaws.com to invoke it. Add the permission with lambda add_permission, using the topic ARN as SourceArn. The console adds this permission automatically, but API and infrastructure-as-code calls do not.