Open In App

AWS Lambda With Amazon S3 Events

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

iam-role

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.

create-lambda-and-attach-policy

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)


code

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

create-trigger

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

upload-object

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

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.

AWS Lambda With Amazon S3 Events – FAQ’s

What is maximum execution time of a Lambda function?

Maximum execution time of a Lambda function is 15 minutes or 900 seconds.

How the Lambda function executes the code?

Lambda function executes the code on any specific event occurrence or it executes at any scheduled time or intervals.

Why IAM policy initially attached to Lambda function?

IAM policy is initially attached to Lambda function to give the permission to Lambda function to access the AWS resource on which action is going to be performed.

How to monitor the output of Lambda function?

You can monitor the output of the Lambda function in the CloudWatch logs.

What are the S3 bucket events?

Creating , deleting , updating objects in S3 buckets are some of the events that is related to S3 bucket.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads