-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython code for lambda
More file actions
50 lines (42 loc) · 1.7 KB
/
python code for lambda
File metadata and controls
50 lines (42 loc) · 1.7 KB
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
45
46
47
48
49
50
import boto3
# Specify the SNS topic ARN
topic_arn = "arn:aws:sns:us-east-1:533267021265:echomail"
# Function to send SNS notifications
def send_sns(message, subject):
try:
client = boto3.client("sns")
result = client.publish(TopicArn=topic_arn, Message=message, Subject=subject)
if result['ResponseMetadata']['HTTPStatusCode'] == 200:
print(result)
print("Notification sent successfully..!!!")
return True
except Exception as e:
print("Error occurred while publishing notifications and the error is:", e)
return False
# Lambda handler function
def lambda_handler(event, context):
print("Event collected is {}".format(event))
for record in event['Records']:
s3_bucket = record['s3']['bucket']['name']
print("Bucket name is {}".format(s3_bucket))
s3_key = record['s3']['object']['key']
print("Bucket key name is {}".format(s3_key))
from_path = "s3://{}/{}".format(s3_bucket, s3_key)
print("From path {}".format(from_path))
message = f"""
Hello Team,
This is to notify you that a new project file has been successfully uploaded to the S3 bucket. Please find the details below:
Bucket Name: {s3_bucket}
File Key: {s3_key}
File Path: {from_path}
If you have any questions or need further assistance, please do not hesitate to contact us.
Best regards,
ECHO-MAIL Project Team
"""
subject ="ECHO-MAIL Project File Upload Notification"
SNSResult = send_sns(message, subject)
if SNSResult:
print("Notification Sent..")
return SNSResult
else:
return False