Modifying the runtime environment
You can use internal extensions to modify the runtime process. Internal extensions are not separate processes—they run as part of the runtime process.
Lambda provides language-specific environment variables that you can set to add options and tools to the runtime. Lambda also provides wrapper scripts, which allow Lambda to delegate the runtime startup to your script. You can create a wrapper script to customize the runtime startup behavior.
Language-specific environment variables
Lambda supports configuration-only ways to enable code to be pre-loaded during function initialization through the following language-specific environment variables:
-
JAVA_TOOL_OPTIONS
– On Java, Lambda supports this environment variable to set additional command-line variables in Lambda. This environment variable allows you to specify the initialization of tools, specifically the launching of native or Java programming language agents using theagentlib
orjavaagent
options. For more information, seeJAVA_TOOL_OPTIONS
environment variable. -
NODE_OPTIONS
– On Node.js 10x and above, Lambda supports this environment variable. -
DOTNET_STARTUP_HOOKS
– On .NET Core 3.1 and above, this environment variable specifies a path to an assembly (dll) that Lambda can use.
Using language-specific environment variables is the preferred way to set startup properties.
Wrapper scripts
You can create a wrapper script to customize the runtime startup behavior of your Lambda function. A wrapper script enables you to set configuration parameters that cannot be set through language-specific environment variables.
Note
Invocations may fail if the wrapper script does not successfully start the runtime process.
Wrapper scripts are supported on all native Lambda runtimes which use the Amazon Linux 2 operating system.
The custom provided.al2
runtime does not support wrapper scripts.
When you use a wrapper script for your function, Lambda starts the runtime using your script. Lambda sends to your script the path to the interpreter and all of the original arguments for the standard runtime startup. Your script can extend or transform the startup behavior of the program. For example, the script can inject and alter arguments, set environment variables, or capture metrics, errors, and other diagnostic information.
You specify the script by setting the value of the AWS_LAMBDA_EXEC_WRAPPER
environment variable
as the file system path of an executable binary or script.
Example: Create and use a wrapper script with Python 3.8
In the following example, you create a wrapper script to start the Python interpreter with the -X
importtime
option. When you run the function, Lambda generates a log entry to show the duration of the
import time for each import.
To create and use a wrapper script with Python 3.8
-
To create the wrapper script, paste the following code into a file named
importtime_wrapper
:#!/bin/bash # the path to the interpreter and all of the originally intended arguments args=("$@") # the extra options to pass to the interpreter extra_args=("-X" "importtime") # insert the extra options args=("${args[@]:0:$#-1}" "${extra_args[@]}" "${args[@]: -1}") # start the runtime with the extra options exec "${args[@]}"
-
To give the script executable permissions, enter
chmod +x importtime_wrapper
from the command line. -
Deploy the script as a Lambda layer.
-
Create a function using the Lambda console.
-
Open the Lambda console
. -
Choose Create function.
-
Under Basic information, for Function name, enter
wrapper-test-function
. -
For Runtime, choose Python 3.8.
-
Choose Create function.
-
-
Add the layer to your function.
-
Choose your function, and then choose Code if it is not already selected.
-
Choose Add a layer.
-
Under Choose a layer, choose the Name and Version of the compatible layer that you created earlier.
-
Choose Add.
-
-
Add the code and the environment variable to your function.
-
In the function code editor, paste the following function code:
import json def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
-
Choose Save.
-
Under Environment variables, choose Edit.
-
Choose Add environment variable.
-
For Key, enter
AWS_LAMBDA_EXEC_WRAPPER
. -
For Value, enter
/opt/importtime_wrapper
. -
Choose Save.
-
-
To run the function, choose Test.
Because your wrapper script started the Python interpreter with the
-X importtime
option, the logs show the time required for each import. For example:... 2020-06-30T18:48:46.780+01:00 import time: 213 | 213 | simplejson 2020-06-30T18:48:46.780+01:00 import time: 50 | 263 | simplejson.raw_json ...