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:

  1. Create S3 Bucket
  2. Create IAM Role
  3. Create Lambda function
  4. Verify email through SES

Let’s start by first creating an S3 bucket where we will upload our objects.

Step 1: Create S3 Bucket

  1. Go to “S3” from the search bar
  1. Click on “Create Bucket” on right upper corner
  1. Give it a name (In this case we are keeping it as “skillcurb-lambda”)
  1. Click Create

Step 2: Create IAM Role

  1. Go to “IAM” from the search bar
  1. Click on “Roles” on the left corner
  1. Click on “Create Role” 
  1. From the use case section click on “Lambda”
  1. Click “Next”
  1. Here, you need to search three things from the Permission Policy search bar:
  • AmazonS3FullAccess
  • AmazonSESFullAccess
  • AmazonCloudWatchFullAccess
  1. Click on “Next” and give your role a name( in this case, we gave it as “skillcurb-lambda-ses”
  1. In the “Add Permissions” section you can see all the three policies that you have attached in the previous step
  1. Click “Create Role”

Step 3: Create Lambda Function

  1. Go to “Lambda” from the search bar on the top
  1. Click on “Create Function” on the upper right corner
  1. Give it a function name(here we have named it as “skillcurb-lambda-trigger”)
  1. In the runtime section, from drop down menu you need to select “Python 3.9”
  1. Next, in the “Permissions” section you need to click on “Use existing role” 
  1. From the drop-down menu you need to select the role you create in the previous step
  1. Click “Create Function”

Step 4: Add trigger through Lambda function

  1. After creating your name function click on “Add trigger” that you can see at the bottom of the lambda function
  1. You need to add trigger by select “S3” from the dropdown menu and select the bucket you created in the first step
  1. Checkbox the recursive invocation box and click on “Add” and trigger will be created
  1. 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']


//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 = """


                 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 

  1. Go to “Amazon Simple Email Services”
  1. On the left corner click on “Verified Identities” and create identity
  1. In this section, select “Email Address” and add the desired email ID that you want to use for this project.
  1. After this, go on the email address that you have provided and click on the link that has been provided.
  1. You can see a green tick when the email is verified.

Step 6: Deploy the lambda function

  1. After all these steps, you can now deploy the lambda function by clicking on “Deploy”
  1. You have to go to the S3 bucket that you have created for this project and upload some file there
  1. Check your Email that you have provided. Make sure to check the spam folder too
  2. 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

  1.  Go on “CloudWatch”
  1. Click on the Logs on the left 
  1. 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:

Notify of
Inline Feedbacks
View all comments