-
Notifications
You must be signed in to change notification settings - Fork 0
/
lamda.py
68 lines (56 loc) · 2.05 KB
/
lamda.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
##This code defines a Lambda function that sends an email notification when triggered ( when the state file change in s3)
import json
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# ===========================
# Start of Lambda Function
# ===========================
print(" Event Received ")
print(json.dumps(event, indent=2)) # Pretty print the event for better readability
ses_client = boto3.client('sesv2')
# ===========================
# Begin Email Sending Process
# ===========================
print(" Initiating Email Sending ")
try:
email_response = ses_client.send_email(
FromEmailAddress='abrarbero2510.dev@gmail.com',
Destination={
'ToAddresses': [
'abrarbero2510@gmail.com',
],
},
Content={
'Simple': {
'Subject': {
'Data': 'Terraform State Update Notification',
'Charset': 'UTF-8'
},
'Body': {
'Text': {
'Data': 'The Terraform state file has been modified.',
'Charset': 'UTF-8'
},
}
},
},
)
# ===========================
# Email Response Logging
# ===========================
print(" Email Sent Successfully ")
print(json.dumps(email_response, indent=2)) # Pretty print the response
except ClientError as error:
# ===========================
# Error Handling for Email Sending
# ===========================
print(f" Error Sending Email ")
print(f"Error Message: {error.response['Error']['Message']}")
# ===========================
# End of Lambda Function
# ===========================
return {
'statusCode': 200,
'body': json.dumps('Upload completed successfully.')
}