Multiple Email Endpoints of a SNS Topic

Viewed 375

I have a Lambda function that gets triggered by a PUT event in a S3 bucket. This function needs to send email to all subscribers of a SNS topic.

Lambda Function code is:

import json
import boto3

print('Loading function')

s3 = boto3.client('s3')
sns = boto3.client('sns')

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    eventname = event['Records'][0]['eventName']
    sns_message = str("A new file has been uploaded in our S3 bucket. Please find the details of file uploaded belown\n\nBUCKET NAME: "+ bucket +"\nFILE NAME: " + key)
    try:
        if eventname == "ObjectCreated:Put":
            subject= "New data available in  S3 Bucket [" + bucket +"]"
            sns_response = sns.publish(TargetArn='<SNS ARN ID>',Message= str(sns_message),Subject= str(subject))
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

However, email is getting triggered only to one email endpoint subscribed to the SNS topic. Any idea what am I missing?

1 Answers

There was an error in my Code. Instead of using TopicArn, I was using TargetArn. Changing the code to TopicArn solved the issue.

Related