Skip to content

Commit 654fb12

Browse files
committed
Added Lambda environment variable
1 parent aeaff07 commit 654fb12

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

awslambdas3copy/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
8282

8383
This role uses the policy `Policy-my-buckets`
8484

85-
* Create an AWS lambda function:
85+
* Create an AWS Lambda function:
8686
* Name: `<LAMBDA_NAME>`
8787
* Runtime: `Java 8`
8888
* Handler: `example.S3CopyHandler::handleRequest`
@@ -103,6 +103,8 @@ It handles an AWS Lambda function that copies an object when it appears in a S3
103103
* Memory (MB): `1024`
104104
* Timeout: `10 sec`
105105

106+
* Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket.
107+
106108
* Upload the Java JAR file.
107109

108110
Artifact:

awslambdas3copy/src/main/java/example/S3CopyHandler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
public class S3CopyHandler implements RequestHandler<S3Event, String> {
2121

22-
private static final String DESTINATION_BUCKET = "targetbucket10"; // Destination bucket name
23-
2422
@Override
2523
public String handleRequest(S3Event s3Event, Context context) {
2624
String sourceBucketName; // Source bucket name
@@ -40,11 +38,16 @@ public String handleRequest(S3Event s3Event, Context context) {
4038
sourceKeyName = record.getS3().getObject().getKey(); // Name doesn't contain any special characters
4139

4240
// Destination Bucket Name
43-
destinationBucketName = DESTINATION_BUCKET;
41+
destinationBucketName = System.getenv("TARGET_BUCKET");
4442

4543
// Destination File Name
4644
destinationKeyName = sourceKeyName;
4745

46+
if (destinationBucketName == null || destinationBucketName.isEmpty()) {
47+
logger.log("Error: TARGET_BUCKET Lambda environment variable does not exist!!");
48+
System.exit(1);
49+
}
50+
4851
logger.log("S3Event: " + s3Event);
4952
logger.log("Source Bucket: " + sourceBucketName + "\n");
5053
logger.log("Source Object: " + sourceKeyName + "\n");

0 commit comments

Comments
 (0)