Skip to content

reserved environment variables constants #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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 <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime">Using AWS Lambda Environment Variables</a>
*
* 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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> 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<String, String> env, String key, String value) {
Expand Down