Integrating Lambda with the Powerful S3 Service: Retrieving Email IDs from S3 bucket and Sending Emails using Boto3 and SES service
Shivang Gupta
Engineer @ Nokia | Network and System Administration, Cybersecurity, Devops
In today's digital age, automation plays a crucial role in streamlining processes and improving efficiency. One such powerful tool for automation is AWS Lambda, which allows you to run code without provisioning or managing servers. When combined with the versatile S3 service and the Boto3 library, Lambda becomes even more potent. In this blog post, we will explore how to integrate Lambda with S3, retrieve email IDs from a file stored in S3, and utilize the Boto3 library to send emails using the SES service for each email ID retrieved.
Step 1: Setting Up the Environment
Before we dive into the implementation details, let's ensure that we have the necessary tools and services set up in our AWS environment. Firstly, make sure you have an AWS account and have the necessary permissions to create and manage Lambda functions, S3 buckets, and SES. Additionally, ensure that you have the Boto3 library installed in your development environment.
lets create an S3 bucket ,choose a unique name for the bucket ,leaving all other setting as default, create the bucket
for SES we need to add an identity(email address) if never done so.
Step 2: Uploading the File to S3
To get started, we need a file in S3 that contains multiple email IDs. This file can be in any format, such as text,CSV or JSON. For simplicity, let's assume we have a text file named "email.txt" containing email IDs. Upload this file to an S3 bucket of your choice. Remember to note down the bucket name and the file's key (i.e., its path within the bucket) as we will need them later.
Step 3: Creating the Lambda Function
Now that we have our file in S3, let's create a Lambda function that will retrieve the email IDs from the file. In the AWS Management Console, navigate to the Lambda service and click on "Create Function." Give your function a name, choose the Python runtime, and select an appropriate execution role that grants necessary permissions for accessing S3 and SES.
Once the function is created, scroll down to the "Function code" section. Here, you can either write your code inline or upload your code. For this example, we will write the code inline using the Python runtime.
领英推荐
Step 4: Retrieving Email IDs from the File
To retrieve the email IDs from the file in S3, we will utilize the Boto3 library, which is the AWS SDK for Python. In the function code section, import the necessary Boto3 modules:
import json
import boto3
def lambda_handler(event, context):
? ? # Create an S3 client
? ? s3 = boto3.client('s3')
? ??
? ? # Retrieve the email IDs from the file in S3
? ? bucket_name = '<your_bucket_name>'
? ? file_name = '<your_file_name>'
? ? response = s3.get_object(Bucket=bucket_name, Key=file_name)
? ? email_ids = response['Body'].read().decode('utf-8').split('\n')
? ??
? ? # Create an SES client
? ? ses = boto3.client('ses')
? ??
? ? # Send emails to the retrieved email IDs
? ? for email_id in email_ids:
? ? ? ? if email_id.strip() != '':
? ? ? ? ? ? response = ses.send_email(
? ? ? ? ? ? ? ? Source='<your_verified_email>',
? ? ? ? ? ? ? ? Destination={
? ? ? ? ? ? ? ? ? ? 'ToAddresses': [
? ? ? ? ? ? ? ? ? ? ? ? email_id.strip()
? ? ? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? Message={
? ? ? ? ? ? ? ? ? ? 'Subject': {
? ? ? ? ? ? ? ? ? ? ? ? 'Data': '<your_subject>'
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? 'Body': {
? ? ? ? ? ? ? ? ? ? ? ? 'Text': {
? ? ? ? ? ? ? ? ? ? ? ? ? ? 'Data': '<your_email_body>'
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? )
? ? ? ? ? ??
? ? ? ? ? ? print(f"Email sent to: {email_id.strip()}")
? ? return {
? ? ? ? 'statusCode': 200,
? ? ? ? ?'body': json.dumps('Emails sent successfully')
? ? }
Make sure to replace:<your_bucket_name> by your own bucket name,<your_file_name> by name of your file uploaded in s3 bucket(email.txt),<your_verified_email> with the email verified with aws SES service,<your_subject> with the desired subject for email and lastly <your_email_body> with whatever message you want to send.
Step 6: Testing the Lambda Function
With the code implementation complete, it's time to test our Lambda function. In the AWS Management Console, navigate to the Lambda service and select your function. Click on "Test" and configure a test event or use a sample event template. Then, click on "Test" again to execute the function.
Check the logs and verify if the email IDs are being retrieved from the file and if emails are being sent successfully using SES.
NOTE: following instructions may not give results if your AWS account runs in sandbox mode,it would be better to request aws for - production access.
Conclusion
In this blog post, we explored how to integrate Lambda with the powerful S3 service. We learned how to retrieve email IDs from a file stored in S3 using the Boto3 library and how to send emails using the SES service for each email ID retrieved. By automating this process, you can save time and effort when it comes to managing and sending emails to a large number of recipients.
AWS Lambda, S3, and SES, combined with the Boto3 library, provide a robust and scalable solution for various automation tasks. Whether it's processing files, sending notifications, or performing data transformations, the possibilities are endless. So go ahead, explore the power of Lambda and unleash the potential of your AWS environment.