adam bien's blog

AWS Lambda Configuration with Java CDK 📎

To fetch an environment variable in an AWS Lambda use the stock method System.getenv:

public class Greetings{
    static String message = System.getenv("message");

    public String onEvent(Map<String, String> input) {
        System.out.println("configured: " + message);
        return message;
    }    
}    

All environment variables are set with the environment method in the function's Builder:


Function createUserListenerFunction(String functionName,String functionHandler, int memory, int timeout) {
    return Function.Builder.create(this, functionName)
            .runtime(Runtime.JAVA_11)
            .code(Code.fromAsset("../target/function.jar"))
            .handler(functionHandler)
            .memorySize(memory)
            .environment(Map.of("message","hello, duke"))
            .functionName(functionName)
            .timeout(Duration.seconds(timeout))
            .build();
}    

The code above is an excerpt from the following screencast:

The project template used in this screencast is available from: https://github.com/AdamBien/aws-lambda-cdk-plain.