|  | 
|  | 1 | +# AWS Lambda Function S3 Copy Python example | 
|  | 2 | + | 
|  | 3 | +This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). | 
|  | 4 | + | 
|  | 5 | +It handles an AWS Lambda function that copies an object when it appears in a S3 bucket to another S3 bucket using the Resource API (high-level) of Boto 3. | 
|  | 6 | + | 
|  | 7 | +## Requirements | 
|  | 8 | + | 
|  | 9 | +* You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. | 
|  | 10 | + | 
|  | 11 | +* The code was written for Python 3 and AWS SDK for Python (Boto 3). | 
|  | 12 | + | 
|  | 13 | +* This example uses Resource API (high-level) of Boto 3. | 
|  | 14 | + | 
|  | 15 | +* The AWS Lambda execution environment include SDK for Python (Boto 3). | 
|  | 16 | + | 
|  | 17 | +## Using the code | 
|  | 18 | + | 
|  | 19 | +* You can select the destination bucket name changing the value of `DESTINATION_BUCKET` variable in the code. | 
|  | 20 | + | 
|  | 21 | +* Access the AWS console. | 
|  | 22 | + | 
|  | 23 | +* Create a S3 bucket for the source and another S3 bucket for the target. | 
|  | 24 | + | 
|  | 25 | +* Create an IAM Policy: ex. `Policy-VM-buckets` | 
|  | 26 | + | 
|  | 27 | + Content of the IAM policy: | 
|  | 28 | + | 
|  | 29 | + ```bash | 
|  | 30 | + { | 
|  | 31 | + "Version": "2012-10-17", | 
|  | 32 | + "Statement": [ | 
|  | 33 | + { | 
|  | 34 | + "Effect": "Allow", | 
|  | 35 | + "Action": [ | 
|  | 36 | + "s3:GetObject" | 
|  | 37 | + ], | 
|  | 38 | + "Resource": [ | 
|  | 39 | + "arn:aws:s3:::sourcevm/*" | 
|  | 40 | + ] | 
|  | 41 | + }, | 
|  | 42 | + { | 
|  | 43 | + "Effect": "Allow", | 
|  | 44 | + "Action": [ | 
|  | 45 | + "s3:PutObject" | 
|  | 46 | + ], | 
|  | 47 | + "Resource": [ | 
|  | 48 | + "arn:aws:s3:::targetvm/*" | 
|  | 49 | + ] | 
|  | 50 | + }, | 
|  | 51 | + { | 
|  | 52 | + "Sid": "Stmt1430872844000", | 
|  | 53 | + "Effect": "Allow", | 
|  | 54 | + "Action": [ | 
|  | 55 | + "cloudwatch:*" | 
|  | 56 | + ], | 
|  | 57 | + "Resource": [ | 
|  | 58 | + "*" | 
|  | 59 | + ] | 
|  | 60 | + }, | 
|  | 61 | + { | 
|  | 62 | + "Sid": "Stmt1430872852000", | 
|  | 63 | + "Effect": "Allow", | 
|  | 64 | + "Action": [ | 
|  | 65 | + "logs:*" | 
|  | 66 | + ], | 
|  | 67 | + "Resource": [ | 
|  | 68 | + "*" | 
|  | 69 | + ] | 
|  | 70 | + } | 
|  | 71 | + ] | 
|  | 72 | + } | 
|  | 73 | + ``` | 
|  | 74 | + | 
|  | 75 | +* Create a role: `Role-VM-buckets`. | 
|  | 76 | + | 
|  | 77 | + This role uses the policy `Policy-VM-buckets` | 
|  | 78 | + | 
|  | 79 | +* Create an AWS lambda function. | 
|  | 80 | + * Name: `<LAMBDA_NAME>` | 
|  | 81 | + * Runtime: `Python 3.6` | 
|  | 82 | + * Handler: `lambda_function.lambda_handler` | 
|  | 83 | + * Role: `Role-VM-buckets` | 
|  | 84 | + * The triggers: | 
|  | 85 | + * `S3` | 
|  | 86 | + * Bucket: `<BUCKET_NAME>` | 
|  | 87 | + * Event type: `ObjectCreated` | 
|  | 88 | + * Enable trigger: `Yes` | 
|  | 89 | + * The resources that the function's role has access to: | 
|  | 90 | + * `Amazon CloudWatch` | 
|  | 91 | + * `Amazon CloudWatch Logs` | 
|  | 92 | + * `Amazon S3` | 
|  | 93 | + * Basic Settings for the lambda function: | 
|  | 94 | + * Memory (MB): `1024` | 
|  | 95 | + * Timeout: `10 sec` | 
|  | 96 | + | 
|  | 97 | +* Write the code. | 
|  | 98 | + | 
|  | 99 | + The content of `lambda_function.py` file. | 
|  | 100 | + | 
|  | 101 | +* Save the Lambda function. | 
|  | 102 | + | 
|  | 103 | + It deploys the Lambda function. | 
|  | 104 | + | 
|  | 105 | +* Test the function: | 
|  | 106 | + | 
|  | 107 | + Copy a file in the source S3 bucket. | 
|  | 108 | + | 
|  | 109 | + The object from the source S3 bucket should be copied to the target S3 bucket. | 
0 commit comments