Integrating AWS Lambda, S3, and SES: Automating Email Sending with Boto3
To integrate AWS Lambda with Amazon S3 and utilize the Boto3 library to retrieve email IDs from a file and send emails using the Simple Email Service (SES), you can follow the steps outlined below.
Step 1: Set up an S3 Bucket
- Go to the AWS Management Console and open the Amazon S3 service.
- Click on “Create bucket” to create a new bucket or select an existing bucket.
- Follow the instructions to configure the bucket settings and permissions.
Step 2: Upload a File with Email IDs to S3
- Upload a file containing email IDs to the S3 bucket you created in Step 1. You can do this manually using the AWS Management Console or programmatically using the AWS SDK or CLI.
Step 3: Set up an IAM Role for Lambda
- Go to the AWS Management Console and open the Identity and Access Management (IAM) service.
- Click on “Roles” and then “Create role.”
- Select the AWS service as “Lambda” and click “Next.”
- Attach the required policies like
AmazonSESFullAccess
andAmazonS3ReadOnlyAccess
to the role. - Provide a name for the role and create it.
Step 4: Create a Lambda Function
- Go to the AWS Management Console and open the AWS Lambda service.
- Click on “Create function” and choose an appropriate function name.
- Select the runtime as “Python” and choose the IAM role you created in Step 3.
- Set the trigger as “S3” and choose the bucket where you uploaded the email ID file.
- Configure the event settings as per your requirements.
- Click on “Create function” to create the Lambda function.
Step 5: Write the Lambda Function Code
- Within the Lambda function, use the Boto3 library to retrieve the email IDs from the S3 file and send emails using SES.
- Below is a sample Python code that demonstrates this functionality:
import boto3
def lambda_handler(event, context):
# Get the S3 bucket and object key from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Retrieve email IDs from the file in S3
s3_client = boto3.client('s3')
response = s3_client.get_object(Bucket=bucket, Key=key)
email_ids = response['Body'].read().decode('utf-8').split('\n')
# Initialize SES client
ses_client = boto3.client('ses')
# Send email for each retrieved email ID
for email_id in email_ids:
email_id = email_id.strip() # Remove leading/trailing whitespaces
# Skip empty lines or invalid email IDs
if not email_id or '@' not in email_id:
continue
# Send email using SES
response = ses_client.send_email(
Source='your-email@example.com', # Sender email address
Destination={'ToAddresses': [email_id]}, # Recipient email address
Message={
'Subject': {'Data': 'Sample Email Subject'},
'Body': {'Text': {'Data': 'Sample Email Body'}}
}
)
print(f"Email sent to: {email_id}. Message ID: {response['MessageId']}")
Note: Replace 'your-email@example.com'
with your actual email address, and modify the subject and body of the email to suit your needs.
Step 6: Configure Lambda Function
- In the Lambda function configuration, set the handler to
lambda_handler
. - Save the Lambda function.
Step 7: Testing and Execution
- You can manually test the Lambda function by uploading a file with email IDs to the configured S3 bucket.
- Check the Lambda function’s CloudWatch Logs for any errors or output messages.
- Each valid email ID should receive an email using the SES service.
Ensure that you have the necessary permissions and credentials set up for the Lambda function to access S3 and SES.