user299709
user299709

Reputation: 5412

AWS Lambda is unable to find a SNS topic that clearly exists?

I create an SNS topic via the console. Then tried to call list_subscriptions_by_topic or sns.publish but they failed with this message:

    An error occurred (NotFound) when calling the Publish 
    operation: Topic does not exist: NotFoundException

The topic is accessible in the SNS console. Any idea why is can't be found?

This is my lambda code:

    from __future__ import print_function
    
    import json
    import boto3
    import random
    
    
    print('Loading function')
    sns = boto3.client('sns')
    
    def lambda_handler(event, context):
        
        response = sns.publish(
            TopicArn='arn:aws:sns:us-west-2:031436316123:topicExists' 
            Message=json.dumps(newMsg),
            MessageAttributes={
                'event_type':{
                    'DataType':'String', 'StringValue':'something'
                    
                }
            }
        )
    
        return response

It is a Lambda trigger set on an SQS that is subscribed to topicExists SNS topic.

Upvotes: 0

Views: 6090

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269081

The problem is that the SNS topic exists in the us-west-2 region, but your SNS client is being created in the us-east-1 region.

This line does not specify a region, so it is being created in us-east-1 by default:

sns = boto3.client('sns')

You should replace it with:

sns = boto3.client('sns', region_name='us-west-2')

Upvotes: 3

Related Questions