S3 trigger AWS Lambda
Tags: cloud, lambda, aws, cloudcomputing
Published: 2023-10-15 05:05:47

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhlw5t0ggbaekvgff6q5.png)


**Steps:-**

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nt2dbesbdjtckf8maat0.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dt1z7sf8nt4skv5h7i67.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58ud5qqxw2haxtg3r0ai.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vr9hh9nh7iradzdfveb.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ohe01kv5vbokakj57ej.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqpfizpemc0m22smdc62.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8di0txlu0dexb7g5rif6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wokqrlwsn8acruouljn8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgzzoum1e8jjm6gqjcwy.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofhhki8gwwb8ij9nzv5w.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x1gbser2ago9ozubn3t.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0oab2u56nsxplhsvbmm.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adwwst0i0x07tfyfss2f.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/084rdd9z6z8gefms8kdm.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ythoxyd4gr18ceoyq9n4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gn0mg0vyr70mgsi1j284.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc4vgjw8oir06vmk93h8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ptjoxkxhqjuymcp3i4q.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9g34fjsf2kk5jj4vxxmo.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t58az850l9mryw9921ss.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93xe6406hb8oxz6y04ww.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzn6t53w7vrscaou9wen.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kb7os2jmrr1ayktlbf3.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9653a4xsbn52vzj2blh.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljfynj611az92q9bahxc.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uj5ibo6hve2a0nrnppxq.png)


**-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/758a1sz7zjcjqhgqoucn.png)


**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.)