I have a Lambda function that gets triggered by a new PUT request in a particular S3 bucket. And sends an email through a SNS topic.
Lambda function code is listed below:
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, 2 emails are getting triggered when a new object is uploaded to the S3 bucket. One with subject programmed via the lambda function, and other one with AWS Notification containing the JSON as listed below.
{"version":"1.0","timestamp":"2021-05-28T13:38:24.548Z","requestContext":{"requestId":"<Request ID>","functionArn":"<Lambda Function ARN>:$LATEST","condition":"Success","approximateInvokeCount":2},"requestPayload":{"Records":[{"eventVersion":"2.1","eventSource":"aws:s3","awsRegion":"us-east-1","eventTime":"2021-05-28T13:35:24.743Z","eventName":"ObjectCreated:Put","userIdentity":{"principalId":"<my ID>"},"requestParameters":{"sourceIPAddress":"<IP Address>"},"responseElements":{"x-amz-request-id":"ABCD","x-amz-id-2":"EFGH"},"s3":{"s3SchemaVersion":"1.0","configurationId":"<Config ID>","bucket":{"name":"<Bucket Name>","ownerIdentity":{"principalId":"<ID>"},"arn":"<Bucket ARN>"},"object":{"key":"<File Key>","size":632088,"eTag":"<Etag>","versionId":"<Version ID>","sequencer":"<Seq>"}}}]},"responseContext":{"statusCode":200,"executedVersion":"$LATEST"},"responsePayload":null}
I want only 1 email to get triggered. How to implement that?
