
In this project we will learn how to setup AWS Lambda triggers and log the details in CloudWatch and send email notification by Amazon SES.
Below is a Solution diagram which explains it better:
Lists of services we are going to use:
- Create S3 Bucket
- Create IAM Role
- Create Lambda function
- Verify email through SES
Let’s start by first creating an S3 bucket where we will upload our objects.
Step 1: Create S3 Bucket
- Go to “S3” from the search bar
- Click on “Create Bucket” on right upper corner
- Give it a name (In this case we are keeping it as “skillcurb-lambda”)
- Click Create
Step 2: Create IAM Role
- Go to “IAM” from the search bar
- Click on “Roles” on the left corner
- Click on “Create Role”
- From the use case section click on “Lambda”
- Click “Next”
- Here, you need to search three things from the Permission Policy search bar:
- AmazonS3FullAccess
- AmazonSESFullAccess
- AmazonCloudWatchFullAccess
- Click on “Next” and give your role a name( in this case, we gave it as “skillcurb-lambda-ses”
- In the “Add Permissions” section you can see all the three policies that you have attached in the previous step
- Click “Create Role”
Step 3: Create Lambda Function
- Go to “Lambda” from the search bar on the top
- Click on “Create Function” on the upper right corner
- Give it a function name(here we have named it as “skillcurb-lambda-trigger”)
- In the runtime section, from drop down menu you need to select “Python 3.9”
- Next, in the “Permissions” section you need to click on “Use existing role”
- From the drop-down menu you need to select the role you create in the previous step
- Click “Create Function”
Step 4: Add trigger through Lambda function
- After creating your name function click on “Add trigger” that you can see at the bottom of the lambda function
- You need to add trigger by select “S3” from the dropdown menu and select the bucket you created in the first step
- Checkbox the recursive invocation box and click on “Add” and trigger will be created
- Use the code below for AWS lambda
import json
import boto3
//Defining our lambda function
def lambda_handler(event, context):
file_name = event['Records'][0]['s3']['object']['key']
bucketName=event['Records'][0]['s3']['bucket']['name']
//Printing details of Event, file name and bucket name
print("Event details : ",event)
print("File Name : ",file_name)
print("Bucket Name : ",bucketName)
//Subject of the Email will be like this
subject = 'Event from ' + bucketName
client = boto3.client("ses")
//Email of the body will be written here
body = """
<br>
This is a notification mail to inform you regarding s3 event.
The file {} is inserted in the {} bucket .
""".format(file_name, bucketName)
message = {"Subject": {"Data": subject}, "Body": {"Html": {"Data": body}}}
//Adding email for source and destination
response = client.send_email(Source = “Add your email address here”, Destination = {"ToAddresses": [“Add your email address here”]}, Message = message)
//Now we will print a line which will be printed in the Log section
print("The mail is sent successfully")
Before you can write the code in the lambda function you need to verify the email address that you are going to use. So go on and verify email ID.
Step 5: Verify Email on SES
- Go to “Amazon Simple Email Services”
- On the left corner click on “Verified Identities” and create identity
- In this section, select “Email Address” and add the desired email ID that you want to use for this project.
- After this, go on the email address that you have provided and click on the link that has been provided.
- You can see a green tick when the email is verified.
Step 6: Deploy the lambda function
- After all these steps, you can now deploy the lambda function by clicking on “Deploy”
- You have to go to the S3 bucket that you have created for this project and upload some file there
- Check your Email that you have provided. Make sure to check the spam folder too
- You can see in the image below the Subject of the mail as well as the body of the mail
Step 7: Check Email details on CloudWatch
- Go on “CloudWatch”
- Click on the Logs on the left
- On the Log events page you will be able to see that the Lambda has been triggered and it’s printing the message you have given in the code.
You can trigger the Lambda function using other Amazon services as well like Amazon DynamoDB, SNS and others. Please leave a comment if you think that this project was helpful or if you have any questions.
SkillCurb YouTube Channel
If you want to see the Video tutorial of this project you can click provided down below: