Accessing AWS Lambda environment variables in Java code

JavaAmazon Web-ServicesAws Lambda

Java Problem Overview


The AWS has introduced Environment variables for accessing in the Lambda function. I could not find any documentation which shows how to access the environment variables from the Lambda function using Java. Can anyone help me?

Java Solutions


Solution 1 - Java

you can get them with:

System.getenv("NAME_OF_YOUR_ENV_VARIABLE")

Solution 2 - Java

If You are using Spring Core then PropertySourcesPlaceholderConfigurer class can be initialized as a part of Configuration and then @Value("${RESOURCE_URL}") annotation can be used to access environment variables.

@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Value("${RESOURCE_URL}")
private String url;

Solution 3 - Java

I am using this -

System.getenv("VAR_NAME");

This works pretty well.

Solution 4 - Java

If you define environment variable in aws lambda with key as "MyKey" and value as "XYX" then I will use:

System.getenv("MyKey");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJava ProgrammerView Question on Stackoverflow
Solution 1 - JavahellomichibyeView Answer on Stackoverflow
Solution 2 - JavadassumView Answer on Stackoverflow
Solution 3 - JavaHimangshu DasView Answer on Stackoverflow
Solution 4 - JavaAkshay KhuleView Answer on Stackoverflow