Manage file Transfer between S3 Buckets using Lambda

Diagram

AWS Lambda Basics Explanation
Function Event: is the data who triggers the lambda function.
Function context: it's properties and methods allow your function to access vital information about its execution environment, which can be crucial for tasks such as logging, error handling, and resource management examples (function_name, invoked_function_arn,aws_request_id, and etc...).
Function environment variables: use it for configuration settings, and reusable function
Layers: Allow you to package libraries and other dependencies to reduce the size of deployment archives and makes it faster to deploy your code.
Differences between Sync and A Sync:
Synchronous Invocation:
The caller waits for the function to process the results.
The function's response is returned directly to the caller.
Suitable for real-time applications where immediate feedback is required.
Asynchronous Invocation:
The caller sends an event to Lambda and gets a quick success response, while Lambda processes the event in the background.
Lambda queues the event for processing and returns a
202 ACCEPTEDstatus code.Ideal for background tasks or operations where immediate results are not critical.
File Structure
Steps
Created Two S3 Bucket ( Internal and External )
Created Assume Policy to allow Lambda use IAM Role that I created, Then created Inline policy that make anyone with this permissions could get, delete, and upload object in S3, Then created resource aws_iam_policy to convert policy to JSON and attach it to the role in aws_iam_role_policy_attachment
Created aws_lambda_function and named it s3-file-transfer-function and put it the of the code lambda.zip, I used environment object for flexible and reusable code in lambda_function.py file, Then created aws_lambda_permission to allow s3 external bucket to trigger lambda, And finally created aws_s3_bucket_notification to create event when object is uploaded in external s3 bucket then trigger lambda function.
You can check whole Task in here ( Github )
Working Examples




Conclusion
I learned how to connect Lambda with S3 buckets. I used an IAM role and policy to allow Lambda to interact with the S3 buckets. Then, I created a Lambda function with the code to transfer files from one bucket to another. Finally, I set permissions and triggers to invoke the Lambda function when a new object is uploaded to the external S3 bucket.
Last updated