How to Set JVM Arguments (VM Options) in AWS Lambda
We can easily set JVM arguments (e.g. system properties) in our AWS Lambda function through a single environment variable.
Let’s say we have a JAR file that we want to run within an AWS Lambda function.
Example: IntelliJ’s VM Options
In IntelliJ, we might use VM Options to specify JVM attributes such as stack/heap memory allocation, system properties, GC flags, etc.
Suppose our project specifies two system properties in VM Options:
-Denv=production -Dport=8080
These may be properties accessed through System.getProperty("env") or System.getProperty("port").
Set properties using JAVA_TOOL_OPTIONS
We can use the JAVA_TOOL_OPTIONS environment variable to specify properties we would normally specify in the command line, or in IntelliJ’s VM Options.
The
JAVA_TOOL_OPTIONSenvironment variable augments the command line particularly for environments in which a command line is not accessible (like AWS Lambdas).JAVA_TOOL_OPTIONSmakes it possible to add these properties independent of the way the JVM is started.
In our AWS Lambda Configuration tab, we can modify Environment variables.
For the key, we can use JAVA_TOOL_OPTIONS.
For the value, we can use -Denv=production -Dport=8080.