2
votes

I am trying to subscribe to an SNS topic with a lambda function as endpoint.

On trying via AWS console, it works perfectly fine. A subscription is added in SNS->Subscriptions which in turn also adds a trigger to lambda function in Lambda->Functions->[function_name]->Triggers

On trying the same thing via boto3 or AWS cli, it adds new subscription, but no trigger gets added in Lambda->Functions->[function_name]->Triggers. As a result lambda function doesn't get triggered for this SNS topic.

import boto3
client = boto3.client('sns')
sns_topic_arn = '<sns_topic_arn>'
lambda_fn_arn = '<lambda_fn_arn>'

response = client.subscribe(TopicArn=sns_topic_arn,Protocol='lambda',
Endpoint=lambda_fn_arn)

I made sure that sns_topic_arn and lambda_fn_arn are correct. Am I missing something?

1

1 Answers

0
votes
  1. Does the lambda endpoint call the ConfirmSubscription action with the token from the confirmation message? Also note that these confirmation tokens are temporary and only valid for three days. Are you refreshing them?

  2. Can you check the response from the subscribe API and see if the response is valid and it has a SubscriptionArn?

  3. Also, have you added the sufficient permissions through add_permission API of the boto3 client so that it can trigger lambda?

Reference: http://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.subscribe for the API documentation and further details