Skip to content

Commit 16f6b99

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

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

Diff for: awslambdas3copy-rapi/README.md

+30-15
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
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
{
@@ -39,7 +44,7 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
3944
"s3:GetObject"
4045
],
4146
"Resource": [
42-
"arn:aws:s3:::sourcevm/*"
47+
"arn:aws:s3:::sourcebucket/*"
4348
]
4449
},
4550
{
@@ -48,11 +53,10 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
4853
"s3:PutObject"
4954
],
5055
"Resource": [
51-
"arn:aws:s3:::targetvm/*"
56+
"arn:aws:s3:::targetbucket/*"
5257
]
5358
},
5459
{
55-
"Sid": "Stmt1430872844000",
5660
"Effect": "Allow",
5761
"Action": [
5862
"cloudwatch:*"
@@ -62,7 +66,6 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
6266
]
6367
},
6468
{
65-
"Sid": "Stmt1430872852000",
6669
"Effect": "Allow",
6770
"Action": [
6871
"logs:*"
@@ -75,15 +78,15 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
7578
}
7679
```
7780

78-
* Create a role: `Role-VM-buckets`.
81+
* Create a role: `Role-my-buckets`.
7982

80-
This role uses the policy `Policy-VM-buckets`
83+
This role uses the policy `Policy-my-buckets`
8184

8285
* Create an AWS lambda function.
8386
* Name: `<LAMBDA_NAME>`
84-
* Runtime: `Python 3.6`
87+
* Runtime: `Python 3.8`
8588
* Handler: `lambda_function.lambda_handler`
86-
* Role: `Role-VM-buckets`
89+
* Role: `Role-my-buckets`
8790
* The triggers:
8891
* `S3`
8992
* Bucket: `<BUCKET_NAME>`
@@ -93,14 +96,15 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
9396
* `Amazon CloudWatch`
9497
* `Amazon CloudWatch Logs`
9598
* `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/*`
99+
* Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`:
100+
* `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*`
101+
* `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*`
100102
* Basic Settings for the lambda function:
101103
* Memory (MB): `1024`
102104
* Timeout: `10 sec`
103105

106+
* Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket.
107+
104108
* Write the code.
105109

106110
The content of `lambda_function.py` file.
@@ -114,3 +118,14 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
114118
Copy a file in the source S3 bucket.
115119

116120
The object from the source S3 bucket should be copied to the target S3 bucket.
121+
122+
You should see the next messages in the log:
123+
124+
```bash
125+
"From - bucket:: <SOURCE_BUCKET_NAME>"
126+
"From - object: <SOURCE_FILE_NAME>"
127+
"To - bucket: <TARGET_BUCKET_NAME>"
128+
"To - object: <TARGET_FILE_NAME>"
129+
"Copying object ..."
130+
"Copied"
131+
```

Diff for: awslambdas3copy-rapi/lambda_function.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
# in a S3 bucket to another S3 bucket.
44
# It uses Resource API (high-level) of Boto3.
55

6+
import os
67
import boto3
78
import botocore
89

910
# Instantiate the service resource object
1011
s3_resource = boto3.resource('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)