DEV Community

Cover image for S3 trigger AWS Lambda
Nishita Mohanty
Nishita Mohanty

Posted on

S3 trigger AWS Lambda

AIM -
Here, The provided AWS Lambda function is part of a serverless project designed to automate the process of copying a specific object from one Amazon S3 bucket to another.

What is AWS Lambda function?
AWS Lambda is a serverless compute service offered by Amazon Web Services (AWS). It allows you to run code without provisioning or managing servers. With AWS Lambda, you can execute your code in response to various events, such as HTTP requests, changes to data in an S3 bucket, updates to a database, or custom events generated by other AWS services.

Brief description:-
The goal is to streamline the data transfer process and ensure that a particular object is always available in a destination bucket for various purposes such as data aggregation, sharing, backup, or further processing.

In order to achieve this via AWS Lambda function, we need to make our source S3 bucket as the trigger which triggers the Lambda function, which then acts according to the code written in it (we have written our code in Python 3.11 language). In our code we have mand performs the following tasks:

-Extracts the object key, source bucket name, and destination bucket name.
-Copies the specified object from the source S3 bucket to the destination S3 bucket.
-Records successful copy operations in the function's logs.
-Handles errors and exceptions, logging them for debugging and monitoring purposes.
In our Lambda function we have attached IAM role too in which we have given full access to S3 bucket.

Services Required:-

  1. AWS Lambda Function: The core component of the project is an AWS Lambda function, which is written in Python 3.11 and uses the Boto3 library to interact with AWS services.
  2. 2 S3 buckets
    • Source S3 Bucket: The source S3 bucket contains the data that needs to be copied.
    • Destination S3 Bucket: The destination S3 bucket is where the object will be copied to.
  3. AWS IAM: Gives full access of S3.

Diagram:-

Image description

Steps:-

- Create 2 S3 buckets and name them as source and destination buckets respectively.
(Note: Follow the standard naming convention)

Image description

Image description

Image description

Create the destination bucket following the same procedure as the source bucket.

Image description

- Create an IAM role and give full-access of S3 Bucket.

Image description

Image description

Image description

Image description

Image description

Image description

- Create a Lambda function and attach the IAM role with it.

Image description

Image description

Image description

- Write the code in the code section and then test it.

Image description

- To test the code click on "test" then click on create event.

Image description

Image description

- After successful test case attach a trigger to the lambda function i.e. the source S3 bucket.

Image description

Image description

Image description

Image description

Image description

Image description

- Then deploy the code.
(At this step the process of syncing is almost completed)
 

Image description

- Now, to test the working (syncing of both the S3's) upload a file in the source S3 bucket.

Image description

Image description

Image description

-At this step we can check on our destination S3 bucket that the file which we had uploaded in our source s3 bucket can be seen here.

Image description

Hence, its shows that our project is running successfully.

Note:-
Clearly mention the object name which you want to upload.
Do, mention its extension file name (example .png, .jpg etc.)

Top comments (0)