diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java index cc8abe28..45301c5d 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java @@ -203,7 +203,7 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro System.setErr(new PrintStream(new LambdaOutputStream(System.err), false, "UTF-8")); setupRuntimeLogger(lambdaLogger); - String runtimeApi = getEnvOrExit("AWS_LAMBDA_RUNTIME_API"); + String runtimeApi = getEnvOrExit(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_RUNTIME_API); LambdaRuntimeClient runtimeClient = new LambdaRuntimeClient(runtimeApi); EnvReader envReader = new EnvReader(); diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java index 8821a113..b2d55204 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java @@ -8,9 +8,9 @@ public class LambdaEnvironment { public static final EnvReader ENV_READER = new EnvReader(); - public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128")); - public static final String LOG_GROUP_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_GROUP_NAME"); - public static final String LOG_STREAM_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_STREAM_NAME"); - public static final String FUNCTION_NAME = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_NAME"); - public static final String FUNCTION_VERSION = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_VERSION"); + public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, "128")); + public static final String LOG_GROUP_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_GROUP_NAME); + public static final String LOG_STREAM_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_STREAM_NAME); + public static final String FUNCTION_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_NAME); + public static final String FUNCTION_VERSION = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_VERSION); } diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/ReservedRuntimeEnvironmentVariables.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/ReservedRuntimeEnvironmentVariables.java new file mode 100644 index 00000000..fc5006c5 --- /dev/null +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/ReservedRuntimeEnvironmentVariables.java @@ -0,0 +1,104 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.amazonaws.services.lambda.runtime.api.client; + +/** + * Lambda runtimes set several environment variables during initialization. + * Most of the environment variables provide information about the function or runtime. + * The keys for these environment variables are reserved and cannot be set in your function configuration. + * @see Using AWS Lambda Environment Variables + * + * NOTICE: This class is forked from io.micronaut.function.aws.runtime.ReservedRuntimeEnvironments found at https://github.com/micronaut-projects/micronaut-aws + * + */ +public interface ReservedRuntimeEnvironmentVariables { + + /** + * The handler location configured on the function. + */ + String HANDLER = "_HANDLER"; + + /** + * The AWS Region where the Lambda function is executed. + */ + String AWS_REGION = "AWS_REGION"; + + /** + * The runtime identifier, prefixed by AWS_Lambda_—for example, AWS_Lambda_java8. + */ + String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV"; + + /** + * The name of the function. + */ + String AWS_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME"; + + /** + * The amount of memory available to the function in MB. + */ + String AWS_LAMBDA_FUNCTION_MEMORY_SIZE = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE"; + + /** + * The version of the function being executed. + */ + String AWS_LAMBDA_FUNCTION_VERSION = "AWS_LAMBDA_FUNCTION_VERSION"; + + /** + * The name of the Amazon CloudWatch Logs group for the function. + */ + String AWS_LAMBDA_LOG_GROUP_NAME = "AWS_LAMBDA_LOG_GROUP_NAME"; + + /** + * The name of the Amazon CloudWatch stream for the function. + */ + String AWS_LAMBDA_LOG_STREAM_NAME = "AWS_LAMBDA_LOG_STREAM_NAME"; + + /** + * Access key id obtained from the function's execution role. + */ + String AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"; + + /** + * secret access key obtained from the function's execution role. + */ + String AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"; + + /** + * + * The access keys obtained from the function's execution role. + */ + String AWS_SESSION_TOKEN = "AWS_SESSION_TOKEN"; + + /** + * (Custom runtime) The host and port of the runtime API. + */ + String AWS_LAMBDA_RUNTIME_API = "AWS_LAMBDA_RUNTIME_API"; + + /** + * The path to your Lambda function code. + */ + String LAMBDA_TASK_ROOT = "LAMBDA_TASK_ROOT"; + + /** + * The path to runtime libraries. + */ + String LAMBDA_RUNTIME_DIR = "LAMBDA_RUNTIME_DIR"; + + /** + * The environment's time zone (UTC). The execution environment uses NTP to synchronize the system clock. + */ + String TZ = "TZ"; +} diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/util/EnvWriter.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/util/EnvWriter.java index 839dddc9..ba090078 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/util/EnvWriter.java +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/util/EnvWriter.java @@ -2,8 +2,11 @@ package com.amazonaws.services.lambda.runtime.api.client.util; +import com.amazonaws.services.lambda.runtime.api.client.ReservedRuntimeEnvironmentVariables; + import java.lang.reflect.Field; import java.util.Map; +import java.util.Optional; import java.util.function.Consumer; public class EnvWriter implements AutoCloseable { @@ -43,24 +46,30 @@ public void setupEnvironmentCredentials() { modifyEnv((env) -> { // AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN are set by the runtime API daemon when // executing the runtime's bootstrap. Ensure these are not empty values. - removeIfEmpty(env, "AWS_ACCESS_KEY_ID"); - removeIfEmpty(env, "AWS_SECRET_ACCESS_KEY"); - removeIfEmpty(env, "AWS_SESSION_TOKEN"); + removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_ACCESS_KEY_ID); + removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_SECRET_ACCESS_KEY); + removeIfEmpty(env, ReservedRuntimeEnvironmentVariables.AWS_SESSION_TOKEN); // The AWS Java SDK supports two alternate keys for the aws access and secret keys for compatibility. // These are not set by the runtime API daemon when executing a runtime's bootstrap so set them here. - addIfNotNull(env, "AWS_ACCESS_KEY", env.get("AWS_ACCESS_KEY_ID")); - addIfNotNull(env, "AWS_SECRET_KEY", env.get("AWS_SECRET_ACCESS_KEY")); + addIfNotNull(env, "AWS_ACCESS_KEY", env.get(ReservedRuntimeEnvironmentVariables.AWS_ACCESS_KEY_ID)); + addIfNotNull(env, "AWS_SECRET_KEY", env.get(ReservedRuntimeEnvironmentVariables.AWS_SECRET_ACCESS_KEY)); }); } public void setupAwsExecutionEnv() { + executionEnvironmentForJavaVersion() + .ifPresent(val -> modifyEnv(env -> env.put(ReservedRuntimeEnvironmentVariables.AWS_EXECUTION_ENV, val))); + } + + private Optional executionEnvironmentForJavaVersion() { String version = System.getProperty("java.version"); if (version.startsWith("1.8")) { - modifyEnv(env -> env.put("AWS_EXECUTION_ENV", "AWS_Lambda_java8")); + return Optional.of("AWS_Lambda_java8"); } else if (version.startsWith("11")) { - modifyEnv(env -> env.put("AWS_EXECUTION_ENV", "AWS_Lambda_java11")); + return Optional.of("AWS_Lambda_java11"); } + return Optional.empty(); } private void addIfNotNull(Map env, String key, String value) {