Skip to content

Commit b374873

Browse files
committed
Added Lambda environment variable
1 parent 16f6b99 commit b374873

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

awslambdas3move-capi/README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
1919

2020
## Using the code
2121

22-
* You can select the destination bucket name changing the value of `DESTINATION_BUCKET` variable in the code.
22+
* You can select the destination bucket name using an AWS Lambda environment variable: `TARGET_BUCKET`
2323

2424
* Access the AWS console.
2525

2626
* Create a S3 bucket for the source and another S3 bucket for the target.
2727

28-
* Create an IAM Policy: ex. `Policy-VM-buckets`
28+
* Create an IAM Policy: ex. `Policy-my-buckets`
2929

30-
Content of the IAM policy:
30+
Changing:
31+
32+
* `sourcebucket` to the name of your source bucket.
33+
* `targetbucket` to the name of your target bucket.
34+
35+
Content of the IAM policy:
3136

3237
```bash
3338
{
@@ -37,9 +42,10 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
3742
"Effect": "Allow",
3843
"Action": [
3944
"s3:GetObject"
45+
"s3:DeleteObject"
4046
],
4147
"Resource": [
42-
"arn:aws:s3:::sourcevm/*"
48+
"arn:aws:s3:::sourcebucket/*"
4349
]
4450
},
4551
{
@@ -48,11 +54,10 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
4854
"s3:PutObject"
4955
],
5056
"Resource": [
51-
"arn:aws:s3:::targetvm/*"
57+
"arn:aws:s3:::targetbucket/*"
5258
]
5359
},
5460
{
55-
"Sid": "Stmt1430872844000",
5661
"Effect": "Allow",
5762
"Action": [
5863
"cloudwatch:*"
@@ -62,7 +67,6 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
6267
]
6368
},
6469
{
65-
"Sid": "Stmt1430872852000",
6670
"Effect": "Allow",
6771
"Action": [
6872
"logs:*"
@@ -75,15 +79,15 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
7579
}
7680
```
7781

78-
* Create a role: `Role-VM-buckets`.
82+
* Create a role: `Role-my-buckets`.
7983

80-
This role uses the policy `Policy-VM-buckets`
84+
This role uses the policy `Policy-my-buckets`
8185

8286
* Create an AWS lambda function.
8387
* Name: `<LAMBDA_NAME>`
84-
* Runtime: `Python 3.6`
88+
* Runtime: `Python 3.8`
8589
* Handler: `lambda_function.lambda_handler`
86-
* Role: `Role-VM-buckets`
90+
* Role: `Role-my-buckets`
8791
* The triggers:
8892
* `S3`
8993
* Bucket: `<BUCKET_NAME>`
@@ -93,14 +97,16 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
9397
* `Amazon CloudWatch`
9498
* `Amazon CloudWatch Logs`
9599
* `Amazon S3`
96-
* Lambda obtained information from the policy statements: `Managed policy Policy-VM-buckets`:
97-
* `s3:GetObject` --> `Allow: arn:aws:s3:::sourcevm/*`
98-
* `s3:DeleteObject` --> `Allow: arn:aws:s3:::sourcevm/*`
99-
* `s3:PutObject` --> `Allow: arn:aws:s3:::targetvm/*`
100+
* Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`:
101+
* `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*`
102+
* `s3:DeleteObject` --> `Allow: arn:aws:s3:::sourcebucket/*`
103+
* `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*`
100104
* Basic Settings for the lambda function:
101105
* Memory (MB): `1024`
102106
* Timeout: `10 sec`
103107

108+
* Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket.
109+
104110
* Write the code.
105111

106112
The content of `lambda_function.py` file.
@@ -114,3 +120,14 @@ It handles an AWS Lambda function that moves an object when it appears in a S3 b
114120
Copy a file in the source S3 bucket.
115121

116122
The object from the source S3 bucket should be copied to the target S3 bucket and deleted in the source S3 bucket.
123+
124+
You should see the next messages in the log:
125+
126+
```bash
127+
"From - bucket:: <SOURCE_BUCKET_NAME>"
128+
"From - object: <SOURCE_FILE_NAME>"
129+
"To - bucket: <TARGET_BUCKET_NAME>"
130+
"To - object: <TARGET_FILE_NAME>"
131+
"Moving object ..."
132+
"Moved"
133+
```

awslambdas3move-capi/lambda_function.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
# in a S3 bucket to another S3 bucket.
44
# It uses Client API (low-level) of Boto3.
55

6+
import os
67
import boto3
78
import botocore
89

910
# Create as S3 Client
1011
s3_client = boto3.client('s3')
1112

1213
def lambda_handler(event, context):
13-
DESTINATION_BUCKET = 'targetvm' # Destination bucket name
14-
1514
source_bucket_name = event['Records'][0]['s3']['bucket']['name']
1615
source_key = event['Records'][0]['s3']['object']['key']
17-
destination_bucket_name = DESTINATION_BUCKET
16+
# Retrieve environment variable TARGET_BUCKET
17+
destination_bucket_name = os.environ.get('TARGET_BUCKET', None)
18+
if destination_bucket_name is None:
19+
# Environment variable TARGET_BUCKET does not exist
20+
print('Error: TARGET_BUCKET Lambda environment variable does not exist!!')
21+
return
1822
destination_key = source_key
1923
print('From - bucket: ' + source_bucket_name)
2024
print('From - object: ' + source_key)

0 commit comments

Comments
 (0)