Home Blogs To integrate AWS Lambda with S3 and utilize the Boto3 library to retrieve email IDs from a file and send emails using the SES service

To integrate AWS Lambda with S3 and utilize the Boto3 library to retrieve email IDs from a file and send emails using the SES service

by Anup Maurya
25 minutes read
To integrate AWS Lambda with S3 and utilize the Boto3 library to retrieve email IDs from a file and send emails using the SES service

In this article we’ll integrate the aws lambda function with aws s3 service using Boto3 library of python language and retrieve the email id’s from the file and send the email’s using aws ses service.

Step1: Set up an S3 bucket

  • Log in to the AWS Management Console.
  • Navigate to the S3 service.
  • Create a new S3 bucket

Step2: Upload a file containing email IDs to the S3 bucket

  • Select the S3 bucket you created.
  • Upload a file that contains email IDs. The file can be in any format, such as CSV, JSON, or plain text.

Step3: Set up an SES configuration

  • Navigate to the AWS Management Console.
  • Go to the Simple Email Service (SES) service.
  • Verify the email addresses you want to use as the “From” address for sending emails. This is a necessary step to comply with SES’s email sending restrictions.

Step4: Create an IAM role for Lambda

  • Navigate to the AWS Management Console.
  • Go to the Identity and Access Management (IAM) service.
  • Create a new IAM role or use an existing one.
  • Attach the necessary policies to the IAM role, such as AmazonS3ReadOnlyAccess and AmazonSESFullAccess.

Step5: Create a Lambda function

  • Navigate to the AWS Management Console.
  • Go to the Lambda service.
  • Create a new Lambda function.
  • Choose the runtime as per your preference (e.g., Python 3.x).
  • Configure the function to use the IAM role you created in the previous step.

Lambda function code

import boto3
import json

def lambda_handler(event, context):
    s3_bucket = 'your-s3-bucket-name'
    s3_key = 'path/to/your/file'
    from_email = 'your-verified-ses-email@example.com'
    subject = 'Email Subject'
    body = 'Email Body'

    # Retrieve email IDs from the file in S3
    s3_client = boto3.client('s3')
    response = s3_client.get_object(Bucket=s3_bucket, Key=s3_key)
    email_ids = response['Body'].read().decode('utf-8').split('\n')

    # Send email for each retrieved email ID
    ses_client = boto3.client('ses')
    for email_id in email_ids:
        if email_id.strip():
            to_email = email_id.strip()

            response = ses_client.send_email(
                Source=from_email,
                Destination={'ToAddresses': [to_email]},
                Message={
                    'Subject': {'Data': subject},
                    'Body': {'Text': {'Data': body}}
                }
            )
            print(f"Email sent to: {to_email}")
  • Replace 'your-s3-bucket-name' with the actual name of your S3 bucket.
  • Replace 'path/to/your/file' with the actual path to the file containing email IDs within the S3 bucket.
  • Replace 'your-verified-ses-email@example.com' with the verified email address you want to use as the “From” address for sending emails.
  • Customize the subject and body variables with your desired email subject and body.

Step6: Configure a test event

  • In the Lambda function configuration, click on “Test” and then “Configure test events”.
  • Choose “Create new test event”.
  • Provide a name for the test event.
  • In the event body, you can use a sample event payload like this,
  • Execute the Lambda function.

The Lambda function will retrieve the email IDs from the specified file in the S3 bucket and send an email to each retrieved email ID using the configured SES service. The console output will show the status of each email sent.

Note: Ensure that you have the necessary permissions and properly configure the AWS CLI or AWS SDK credentials with the required access to S3 and SES.

related posts

Leave a Comment