How to set system properties for a Lambda function written in Java

AWS Lambda, like other environments, doesn’t make accessible the command line used to start the Java virtual machine. Thus, it’s a little bit more complicated to add the running options to your application or the input parameters, specified as environment variable, or, preferable, system properties. More details about these 2 concepts are available in the official Java documentation.

Therefore, if you want to extend the command line used to start the JVM, the JAVA_TOOL_OPTIONS environment variable is the solution. We also presenting it as a solution to augment an Elastic Beanstalk environment and the conclusion is the same: lack of documentation in the AWS space.

Enough of this twaddle, let’s see a concrete example.

1. The Lambda function code:

package com.cirrustech.lambda;

import com.amazonaws.services.lambda.runtime.Context;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SampleLambdaFunction {
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        context.getLogger().log("Starting the Lambda function...");
        context.getLogger().log("Getting a value: " + System.getProperty("foo"));
        context.getLogger().log("Check value exists: " + (System.getProperty("foo2") != null));
        context.getLogger().log("Execution completed...");
    }
}

2. The Lambda function setup in the AWS console:

3. And the test output:

YY:08:26 Picked up JAVA_TOOL_OPTIONS: -Dfoo=bar 
YY:08:27 START RequestId: AAAAAAAA-4c19-11e7-a8ac-XXXXXXXXXXXX Version: $LATEST 
YY:08:27 Starting the Lambda function... 
YY:08:27 Getting a value: bar 
YY:08:27 Check value exists: false 
YY:08:27 Execution completed... 
YY:08:27 END RequestId: AAAAAAAA-4c19-11e7-a8ac-XXXXXXXXXXXX 
YY:08:27 REPORT RequestId: AAAAAAAA-4c19-11e7-a8ac-XXXXXXXXXXXX Duration: 4.20 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 24 MB

We hope you find this post useful! If you have any question, let us know in the comment section of the blog!

3 thoughts on “How to set system properties for a Lambda function written in Java

    1. Thank you for your kind words! Your comment makes us continue our work. Please don’t hesitate to subscribe and to like our new Facebook page.

Comments are closed.