-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambdafunc.py
44 lines (39 loc) · 1.99 KB
/
lambdafunc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# ------------------------------------- PLEASE READ ------------------------------------------
# PURPOSE: This Python script is intended to be used as a AWS Lambda function with
# Runtime Version Python3.7. When integrated with AWS S3 Bucket and SNS Topic. It report
# through Email that changes(Update/deletion) has been made into the file in S3 bucket
#
# VERY IMPORTANT - REPLACE THE TEXT ENCLOSED IN BRACKETS <> on line#43 WITH YOUR SNS ARN
# THE IMPLEMENTATION OF SCRIPT IS DEMONSTRATED AT YOUTUBE URL - <PLACEHOLDER>
#
# Please feel free to update script as per your usage and it is highly recommended that user READ
# and UNDERSTAND the script and its purpose prior to the execution.
# ------------------------------------------------------------------------------------------------
import json
import urllib.parse
import boto3
print('Loading function')
s3 = boto3.client('s3')
sns = boto3.client('sns')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object, Key and eventName from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
eventname = event['Records'][0]['eventName']
sns_message = str("This Email Represent a File Status has been Changed in One of Your Bucket \n\n BUCKET NAME: "+ bucket +"\n\n FILE NAME: " + key + "\n\n OPERATION: " + eventname + "\n\n")
try:
print(eventname)
print(str(sns_message))
subject= "S3 Bucket[" + bucket + "] Event[" + eventname + "]"
print(subject)
sns_response = sns.publish(
TargetArn='arn:aws:sns:us-east-1:ACCOUNTNAME:sns-siri',
Message= str(sns_message),
Subject= str(subject)
)
#return response['ContentType']
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