Skip to content

Commit 752e012

Browse files
sdelamocarlzogh
authored andcommitted
reserved environment variables constants
encapsulate reserved environment variables names
1 parent 8c76130 commit 752e012

File tree

4 files changed

+126
-13
lines changed

4 files changed

+126
-13
lines changed

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
203203
System.setErr(new PrintStream(new LambdaOutputStream(System.err), false, "UTF-8"));
204204
setupRuntimeLogger(lambdaLogger);
205205

206-
String runtimeApi = getEnvOrExit("AWS_LAMBDA_RUNTIME_API");
206+
String runtimeApi = getEnvOrExit(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_RUNTIME_API);
207207
LambdaRuntimeClient runtimeClient = new LambdaRuntimeClient(runtimeApi);
208208

209209
EnvReader envReader = new EnvReader();

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public class LambdaEnvironment {
1010
public static final EnvReader ENV_READER = new EnvReader();
11-
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"));
12-
public static final String LOG_GROUP_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_GROUP_NAME");
13-
public static final String LOG_STREAM_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_STREAM_NAME");
14-
public static final String FUNCTION_NAME = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_NAME");
15-
public static final String FUNCTION_VERSION = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_VERSION");
11+
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, "128"));
12+
public static final String LOG_GROUP_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_GROUP_NAME);
13+
public static final String LOG_STREAM_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_STREAM_NAME);
14+
public static final String FUNCTION_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_NAME);
15+
public static final String FUNCTION_VERSION = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_VERSION);
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2017-2020 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.amazonaws.services.lambda.runtime.api.client;
17+
18+
/**
19+
* Lambda runtimes set several environment variables during initialization.
20+
* Most of the environment variables provide information about the function or runtime.
21+
* The keys for these environment variables are reserved and cannot be set in your function configuration.
22+
* @see <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime">Using AWS Lambda Environment Variables</a>
23+
*
24+
* NOTICE: This class is forked from io.micronaut.function.aws.runtime.ReservedRuntimeEnvironments found at https://github.com/micronaut-projects/micronaut-aws
25+
*
26+
*/
27+
public interface ReservedRuntimeEnvironmentVariables {
28+
29+
/**
30+
* The handler location configured on the function.
31+
*/
32+
String HANDLER = "_HANDLER";
33+
34+
/**
35+
* The AWS Region where the Lambda function is executed.
36+
*/
37+
String AWS_REGION = "AWS_REGION";
38+
39+
/**
40+
* The runtime identifier, prefixed by AWS_Lambda_—for example, AWS_Lambda_java8.
41+
*/
42+
String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV";
43+
44+
/**
45+
* The name of the function.
46+
*/
47+
String AWS_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME";
48+
49+
/**
50+
* The amount of memory available to the function in MB.
51+
*/
52+
String AWS_LAMBDA_FUNCTION_MEMORY_SIZE = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE";
53+
54+
/**
55+
* The version of the function being executed.
56+
*/
57+
String AWS_LAMBDA_FUNCTION_VERSION = "AWS_LAMBDA_FUNCTION_VERSION";
58+
59+
/**
60+
* The name of the Amazon CloudWatch Logs group for the function.
61+
*/
62+
String AWS_LAMBDA_LOG_GROUP_NAME = "AWS_LAMBDA_LOG_GROUP_NAME";
63+
64+
/**
65+
* The name of the Amazon CloudWatch stream for the function.
66+
*/
67+
String AWS_LAMBDA_LOG_STREAM_NAME = "AWS_LAMBDA_LOG_STREAM_NAME";
68+
69+
/**
70+
* Access key id obtained from the function's execution role.
71+
*/
72+
String AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID";
73+
74+
/**
75+
* secret access key obtained from the function's execution role.
76+
*/
77+
String AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY";
78+
79+
/**
80+
*
81+
* The access keys obtained from the function's execution role.
82+
*/
83+
String AWS_SESSION_TOKEN = "AWS_SESSION_TOKEN";
84+
85+
/**
86+
* (Custom runtime) The host and port of the runtime API.
87+
*/
88+
String AWS_LAMBDA_RUNTIME_API = "AWS_LAMBDA_RUNTIME_API";
89+
90+
/**
91+
* The path to your Lambda function code.
92+
*/
93+
String LAMBDA_TASK_ROOT = "LAMBDA_TASK_ROOT";
94+
95+
/**
96+
* The path to runtime libraries.
97+
*/
98+
String LAMBDA_RUNTIME_DIR = "LAMBDA_RUNTIME_DIR";
99+
100+
/**
101+
* The environment's time zone (UTC). The execution environment uses NTP to synchronize the system clock.
102+
*/
103+
String TZ = "TZ";
104+
}

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/util/EnvWriter.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
package com.amazonaws.services.lambda.runtime.api.client.util;
44

5+
import com.amazonaws.services.lambda.runtime.api.client.ReservedRuntimeEnvironmentVariables;
6+
57
import java.lang.reflect.Field;
68
import java.util.Map;
9+
import java.util.Optional;
710
import java.util.function.Consumer;
811

912
public class EnvWriter implements AutoCloseable {
@@ -43,24 +46,30 @@ public void setupEnvironmentCredentials() {
4346
modifyEnv((env) -> {
4447
// AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN are set by the runtime API daemon when
4548
// executing the runtime's bootstrap. Ensure these are not empty values.
46-
removeIfEmpty(env, "AWS_ACCESS_KEY_ID");
47-
removeIfEmpty(env, "AWS_SECRET_ACCESS_KEY");
48-
removeIfEmpty(env, "AWS_SESSION_TOKEN");
49+
removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_ACCESS_KEY_ID);
50+
removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_SECRET_ACCESS_KEY);
51+
removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_SESSION_TOKEN);
4952

5053
// The AWS Java SDK supports two alternate keys for the aws access and secret keys for compatibility.
5154
// These are not set by the runtime API daemon when executing a runtime's bootstrap so set them here.
52-
addIfNotNull(env, "AWS_ACCESS_KEY", env.get("AWS_ACCESS_KEY_ID"));
53-
addIfNotNull(env, "AWS_SECRET_KEY", env.get("AWS_SECRET_ACCESS_KEY"));
55+
addIfNotNull(env, "AWS_ACCESS_KEY", env.get(ReservedRuntimeEnvironmentVariables.AWS_ACCESS_KEY_ID));
56+
addIfNotNull(env, "AWS_SECRET_KEY", env.get(ReservedRuntimeEnvironmentVariables.AWS_SECRET_ACCESS_KEY));
5457
});
5558
}
5659

5760
public void setupAwsExecutionEnv() {
61+
executionEnvironmentForJavaVersion()
62+
.ifPresent(val -> modifyEnv(env -> env.put(ReservedRuntimeEnvironmentVariables.AWS_EXECUTION_ENV, val)));
63+
}
64+
65+
private Optional<String> executionEnvironmentForJavaVersion() {
5866
String version = System.getProperty("java.version");
5967
if (version.startsWith("1.8")) {
60-
modifyEnv(env -> env.put("AWS_EXECUTION_ENV", "AWS_Lambda_java8"));
68+
return Optional.of("AWS_Lambda_java8");
6169
} else if (version.startsWith("11")) {
62-
modifyEnv(env -> env.put("AWS_EXECUTION_ENV", "AWS_Lambda_java11"));
70+
return Optional.of("AWS_Lambda_java11");
6371
}
72+
return Optional.empty();
6473
}
6574

6675
private void addIfNotNull(Map<String, String> env, String key, String value) {

0 commit comments

Comments
 (0)