AWS Lambda With Amazon S3 Events
Last Updated :
15 Mar, 2024
AWS Lambda and AWS S3 are two important AWS services used by organizations. AWS Lambda is a serverless compute service that executes the code without any management of servers by the user, while AWS S3 is an object-storing service. In this guide, I have first discussed what is AWS Lambda and then I have discussed what the AWS S3 service is. After this, I walked you through the different steps to create a lambda function and execute the lambda function based on any S3 bucket event that occurs.
What is AWS Lambda?
AWS Lambda is a serverless computing service that allows users to execute their code without managing servers. AWS Lambda supports many programming languages, such as Python, Golang, Java, Node.js, etc. Through the AWS Lambda service, users can create their own Lambda function. These Lambda functions can execute based on any specific event occurrence or at any scheduled time or interval. For example, suppose an organization is using many AWS EC2 instances. Some of the EC2 instances are not used for a long period. These unnecessary EC2 instances will incur more costs. So to solve this issue, organizations make their own custom Lambda function that will automatically delete the unused EC2 instances after crossing a certain threshold time. Apart from this, the Lambda function can also automatically scale up or scale down depending on the amount of traffic it receives. In summary, we can say AWS Lambda provides a flexible, cost-effective, and scalable platform to run the code without any server management, which helps in the development of serverless applications on the AWS cloud platform.
What is AWS S3?
AWS S3 is an object storage service that helps in storing and managing data on the AWS cloud platform. S3 buckets are very similar to file and folder systems on the computer. These S3 buckets mainly store images, videos, static websites, and many more. AWS S3 buckets are highly durable. Every S3 bucket has a globally unique name.
The S3 bucket provides many features to manage and organize the data. Some features are:
- S3 allows users to block public access to the S3 buckets.
- It has a versioning feature that allows the storage of multiple versions of an object in the S3 bucket.
- It allows object replication across the region or the same region to ensure high availability of data.
- It is horizontally scaled to handle massive data and requests.
- S3 provides various storage classes, such as S3 Glacier, S3 Standard, S3 Standard IA, and many more. Users can select the storage class as per their requirements.
Pre-requisites
Before moving to next section make sure that you go through these geeks for geeks article to create S3 bucket and IAM role . If you know already how to create S3 bucket and IAM role , then move to next section .
Steps To Execute Lambda Function With Amazon S3 Bucket Events
Step 1: Go to AWS console and search S3 service . Here create a new S3 bucket.

Step 2: Then go to AWS IAM service to create an IAM role for the Lambda . Here attach the S3 full access policy and CloudWatch logs full access policy.

Step 3: Now go to AWS Lambda service , here create a Lambda function. Here i have selected python 3.10 as runtime and also attached the IAM role created in step 2.

Step 4: Now write the code which will get executed on the basis of any S3 event.
import json
def lambda_handler(event, context):
record = event['Records'][0]
event_name = record['eventName']
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
message = f"Event : '{event_name}' . Object '{key}' in bucket '{bucket}'"
print(message)

Step 5: Create a S3 trigger to the Lambda function. Here i have selected the object create event.

Step 6: Now upload any demo file to S3 bucket.

Step 7: Now go to the Lambda function and here click monitor. Then watch the output of the code in the CloudWatch logs.

Conclusion
Here in this guide you have first learned about the AWS Lambda. Then you have learned about AWS S3. After this you have created a S3 bucket, a Lambda function and S3 event trigger. In the Lambda function you have written a python code which will get executed as per the events of S3 bucket. Finally you have observed the output of the Lambda function in the CloudWatch logs.
Similar Reads
AWS - Amazon EventBridge
Amazon EventBridge is a fully managed serverless event bus service provided by AWS that allows you to build event-driven applications. It enables communication between different AWS services, custom applications, and third-party SaaS applications using events. With EventBridge, you can easily route
13 min read
AWS Lambda Functions With AWS CLI
Amazon Web Services (AWS) is a comprehensive cloud computing platform offering many services, including storage, computing, databases, and more. AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). we can create functions and self-contained applications. With AWS Lambd
5 min read
AWS (Amazon Web Services) SNS VS SQS
Are you confused between the AWS SNS and AWS SQS? If yes, here is the best guide to help you. This insightful article covers complete details on the comparison between the AWS SNS and AWS SQS. By the end of this guide, you will be able to easily find what AWS Tool is best suited for your business. I
8 min read
AWS Lambda - Copy Object Among S3 Based on Events
In this article, we will make AWS Lambda function to copy files from one s3 bucket to another s3 bucket. The lambda function will get triggered upon receiving the file in the source bucket. We will make use of Amazon S3 Events. Every file when uploaded to the source bucket will be an event, this nee
3 min read
A Basic AWS Lambda Example With Java
AWS Lambda is a powerful serverless computing service that allows you to run code without provisioning or managing servers. It automatically scales your application by running code in response to triggers such as changes to data in an Amazon S3 bucket, updates to a DynamoDB table, or HTTP requests v
8 min read
Integrating AWS Lambda with Application Load Balancer
With the growing acceptance of the serverless architecture within businesses, AWS Lambda is a computing service that is being applied broadly for running functions without provisioning or managing servers. However, maintaining balance in the traffic reaching these Lambda functions is one critical fa
6 min read
Amazon Web Services (AWS) Tutorial
Amazon Web Services(AWS) is one of the world's most adopted cloud computing platform that offers Infrastructure as a Service(IaaS) and Platform as a Service(PaaS). AWS offers on-demand computing services, such as virtual servers and storage, that can be used to build and run applications and website
9 min read
AWS Lambda Deployments with AWS CloudFormation
AWS CloudFormation allows to create AWS resources with help of templates. It provisions and deploy all the resources required for the entire application described as a Stack. Templates can be updated and replicated as required to reduce the overhead of implementing and provisioning infrastructure. l
6 min read
How to get AWS Account Id in Lambda
AWS Lambda is a FaaS (Function as a Service) provided by Amazon Web Services. It is a compute service which can be used to run code in response to an event without provisioning or managing servers making it an optimal choice for creating event-driven serverless applications. AWS Lambda provides high
6 min read
Amazon Web Services - Creating a Lambda Function
Pre-Requisites: AWS AWS Lambda is one of the compute services that helps you to run code without provisioning or managing resources servers. With lambda, user can run their code on a high-availability computing infrastructure. Lambda performs all the tasks related to the administration of the comput
5 min read