in samcli/commands/local/lib/local_lambda.py [0:0]
def _make_env_vars(self, function: Function) -> EnvironmentVariables:
"""Returns the environment variables configuration for this function
Priority order for environment variables (high to low):
1. Function specific env vars from json file
2. Global env vars from json file
Parameters
----------
function : samcli.commands.local.lib.provider.Function
Lambda function to generate the configuration for
Returns
-------
samcli.local.lambdafn.env_vars.EnvironmentVariables
Environment variable configuration for this function
Raises
------
samcli.commands.local.lib.exceptions.OverridesNotWellDefinedError
If the environment dict is in the wrong format to process environment vars
"""
function_id = function.function_id
logical_id = function.name
function_name = function.functionname
full_path = function.full_path
variables = None
if isinstance(function.environment, dict) and "Variables" in function.environment:
variables = function.environment["Variables"]
else:
LOG.debug("No environment variables found for function '%s'", logical_id)
# This could either be in standard format, or a CloudFormation parameter file format, or mix of both.
#
# Standard format is {FunctionName: {key:value}, FunctionName: {key:value}}
# CloudFormation parameter file is {"Parameters": {key:value}}
# Mixed format is {FunctionName: {key:value}, "Parameters": {key:value}}
for env_var_value in self.env_vars_values.values():
if not isinstance(env_var_value, dict):
reason = "Environment variables {} in incorrect format".format(env_var_value)
LOG.debug(reason)
raise OverridesNotWellDefinedError(reason)
overrides = {}
# environment variables for specific resources take precedence over
# the single environment variable for all resources
if "Parameters" in self.env_vars_values:
LOG.debug("Environment variables data found in the CloudFormation parameter file format")
# CloudFormation parameter file format
parameter_result = self.env_vars_values.get("Parameters", {})
overrides.update(parameter_result)
# Precedence: logical_id -> function_id -> function name -> full_path, customer can use any of them
fn_file_env_vars = (
self.env_vars_values.get(logical_id, None)
or self.env_vars_values.get(function_id, None)
or self.env_vars_values.get(function_name, None)
or self.env_vars_values.get(full_path, None)
)
if fn_file_env_vars:
# Standard format
LOG.debug("Environment variables data found for specific function in standard format")
overrides.update(fn_file_env_vars)
shell_env = os.environ
aws_creds = self.get_aws_creds()
return EnvironmentVariables(
function.name,
function.memory,
function.timeout,
function.handler,
function.logging_config,
variables=variables,
shell_env_values=shell_env,
override_values=overrides,
aws_creds=aws_creds,
) # EnvironmentVariables is not yet annotated with type hints, disable mypy check for now. type: ignore