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.
AWS Lambda With Amazon S3 Events
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.