Skip to content

Commit 85d415a

Browse files
committed
First commit
1 parent 808fe37 commit 85d415a

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

awslambdas3move-capi/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# AWS Lambda Function S3 Move 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 moves an object when it appears in a S3 bucket to another S3 bucket using the Client API (low-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 Client API (low-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 and deleted in the source S3 bucket.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# lamdda_function.py
2+
# It handles an AWS Lambda function that moves an object when it appears
3+
# in a S3 bucket to another S3 bucket.
4+
# It uses Client API (low-level) of Boto3.
5+
6+
import boto3
7+
import botocore
8+
9+
# Create as S3 Client
10+
s3client = boto3.client('s3')
11+
12+
def lambda_handler(event, context):
13+
DESTINATION_BUCKET = 'targetvm' # Destination bucket name
14+
15+
source_bucket_name = event['Records'][0]['s3']['bucket']['name']
16+
source_key = event['Records'][0]['s3']['object']['key']
17+
destination_bucket_name = DESTINATION_BUCKET
18+
destination_key = source_key
19+
print('From - bucket: ' + source_bucket_name)
20+
print('From - object: ' + source_key)
21+
print('To - bucket: ' + destination_bucket_name)
22+
print('To - object: ' + destination_key)
23+
24+
try:
25+
# Copy the object
26+
print('Moving object ...')
27+
copy_source = {
28+
'Bucket': source_bucket_name,
29+
'Key': source_key
30+
}
31+
s3client.copy(copy_source, destination_bucket_name, destination_key)
32+
# Delete the object from source bucket
33+
response = s3client.delete_object(Bucket=source_bucket_name, Key=source_key)
34+
print(response)
35+
print('\nMoved')
36+
except botocore.exceptions.ClientError as e:
37+
if e.response['Error']['Code'] == "AccessDenied":
38+
print("Error: Access denied!!")
39+
elif e.response['Error']['Code'] == "InvalidBucketName":
40+
print("Error: Invalid bucket name!!")
41+
elif e.response['Error']['Code'] == "NoSuchBucket":
42+
print("Error: No such bucket!!")
43+
else:
44+
raise

0 commit comments

Comments
 (0)