This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services).
It handles an AWS Lambda function that copies an object when it appears in a S3 bucket to another S3 bucket using the Client API (low-level) of Boto 3.
-
You must have an Amazon Web Services (AWS) account.
-
The code was written for:
- Python 3
- AWS SDK for Python (Boto3)
-
This example uses Client API (low-level) of Boto 3.
-
The AWS Lambda execution environment include SDK for Python (Boto 3).
-
You can select the destination bucket name using an AWS Lambda environment variable:
TARGET_BUCKET
-
Access the AWS console.
-
Create a S3 bucket for the source and another S3 bucket for the target.
-
Create an IAM Policy: ex.
Policy-my-buckets
Changing:
sourcebucket
to the name of your source bucket.targetbucket
to the name of your target bucket.
Content of the IAM policy:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::sourcebucket/*" ] }, { "Effect": "Allow", "Action": [ "s3:PutObject" ], "Resource": [ "arn:aws:s3:::targetbucket/*" ] }, { "Effect": "Allow", "Action": [ "cloudwatch:*" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "logs:*" ], "Resource": [ "*" ] } ] }
-
Create a role:
Role-my-buckets
.This role uses the policy
Policy-my-buckets
-
Create an AWS lambda function.
- Name:
<LAMBDA_NAME>
- Runtime:
Python 3.8
- Handler:
lambda_function.lambda_handler
- Role:
Role-my-buckets
- Runtime Settings for the lambda function:
- Memory (MB):
1024
- Timeout:
10 sec
- Memory (MB):
- The resources that the function's role has access to:
Amazon CloudWatch
Amazon CloudWatch Logs
Amazon S3
- Lambda obtained information from the policy statements:
Managed policy Policy-my-buckets
:s3:GetObject
-->Allow: arn:aws:s3:::sourcebucket/*
s3:PutObject
-->Allow: arn:aws:s3:::targetbucket/*
- Lambda obtained information from the policy statements:
- The triggers:
S3
- Bucket:
<BUCKET_NAME>
- Event type:
ObjectCreated
- Enable trigger:
Yes
- Bucket:
- Name:
-
Create the AWS Lambda environment variable
TARGET_BUCKET
and set its value to the name of your target bucket. -
Write the code.
The content of
lambda_function.py
file. -
Save the Lambda function.
It deploys the Lambda function.
-
Test the function:
Copy a file in the source S3 bucket.
The object from the source S3 bucket should be copied to the target S3 bucket.
You should see the next messages in the log:
"From - bucket:: <SOURCE_BUCKET_NAME>" "From - object: <SOURCE_FILE_NAME>" "To - bucket: <TARGET_BUCKET_NAME>" "To - object: <TARGET_FILE_NAME>" "Copying object ..." "Copied"