AWS Lambda Event Notifications allow you to receive notifications when certain events happen in your Amazon S3 bucket. S3 Event Notifications can be used to initiate the Lambda functions, SQS queues, and other AWS services. The benefit of using S3 Event
Notifications is that it allows you to automate tasks and processes that would otherwise need to be done manually, improving efficiency and reducing errors. This article will cover how a lambda function gets invoked when an object is uploaded in an S3. Before seeing the practical implementation of how to use an Amazon S3 trigger to invoke a lambda function, let’s have a brief introduction to Amazon S3 and Lambda Functions.
Learning Objectives
This article was published as a part of the Data Science Blogathon.
Amazon S3 (Simple Storage Service) is a cloud-based storage service that allows users to store and retrieve data through the internet. It is used because it is highly scalable, reliable, and inexpensive. This makes it a popular choice for storing and managing large amounts of data, backups, media files, and data for web and mobile applications. Additionally, S3 provides features such as data durability, versioning, and security, which makes it a suitable option for a wide range of use cases, including data archiving disaster recovery, and big data analytics. Amazon Simple Storage Service(S3) provides object-level storage. It stores data in the form of an object. To store your data on S3 you have to first create an S3 bucket and the name should be globally unique. You can not create nested buckets i.e. you can not create buckets inside buckets. Amazon S3 uses standard-based REST API. Some of the important key points of Amazon S3 are:
AWS Lambda is a computing service for serverless. Serverless means that your code runs on servers, but you don’t have to manage the servers.
One of the important benefits of cloud computing is its ability to abstract the infrastructure layer. This eliminates the need to manually manage the underlying infrastructure or instances that are hosting your applications. By building serverless applications you can focus on the code that makes your business more impactful. Here is an overview of howLambda works:
Let’s see how to create a basic lambda function from the console.
Step 1: Go to AWS Console, search for Lambda, and choose the Create function.
Step 2: Select Author from scratch, give some function name, and choose the Runtime Environment. Leave the rest of the options as default.
Step 3: Click on the Test tab to invoke a function from the console.
Step 4: For the test event, create a new event and give an Event name. For Event sharing settings, choose Private, and for Template, leave it to default.
Step 5: Save it and then click on Test. You can see the logs from the Monitor tab. It will show the logs which Lambda sends to CloudWatch.
Create a bucket
Step 1: Open AWS Console and search for S3. Click on Create bucket.
Step 2: Under General Configuration, enter a unique bucket name and select the AWS Region. Leave the rest of the options as it is and click on Create bucket.
Note: The bucket AWS Region and Lambda function AWS Region should be the same.
Modify the existing Lambda Function and Create an IAM Role for Lambda Function
Step 1: Click on Add Trigger and select s3 from the list. Select All objects to create events in the Event type.
Step 2: Create an IAM Role for Full S3 Access and Full CloudWatch Access. From IAM Roles, click on Create role. Select AWS service from the Trusted entity type and select Lambda from the Use case. Click on Next.
Step 3: From Permission policies, select AmazonS3FullAccess and CloudWatchFullAccess and click on Next.
Step 4: Give the Role name and leave the rest of the fields with a default value. Click on Create role.
Step 5: Once the role is created, go to your existing lambda function and add Permission.
Step 6: Go to the lambda_handler function and change the existing code. To access the s3 object, you need Boto3. Boto3 is Python SDK to access AWS services, and by default, it comes with a lambda function, so you don’t have to install it explicitly.
import boto3 from urllib.parse import unquote_plus def lambda_handler(event, context): """Read file from s3 on trigger.""" s3 = boto3.client("s3") if event: file_obj = event["Records"][0] bucketname = str(file_obj["s3"]["bucket"]["name"]) filename = unquote_plus(str(file_obj["s3"]["object"]["key"])) print("Filename: ", filename) fileObj = s3.get_object(Bucket=bucketname, Key=filename) file_content = fileObj["Body"].read().decode("utf-8") print(file_content) return "File is uploaded in S3"
Step 7: Upload a file in your bucket.
Step 8: Go to CloudWatch and check the logs.
This article shows how you can deploy your code on AWS Lambda. By using AWS Lambda you can focus more on your business logic rather than worrying about how to manage the underlying infrastructure or instances. You pay only for the compute time,i.e. only for the time your code runs. By using an event-driven architecture with
S3, you can build more automated and scalable workflows for your data and easily integrate S3 with other AWS services to create powerful, serverless applications.
Some of the key takeaways from the article are:
I hope you liked the article. You can connect with me on Linkedin for further discussion or comment below. HappyLearning 🙂
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.