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?
Asked
Active
Viewed 3.5k times
68
4 Answers
117
you can get them with:
System.getenv("NAME_OF_YOUR_ENV_VARIABLE")

Shashank Agrawal
- 25,161
- 11
- 89
- 121

hellomichibye
- 4,112
- 2
- 23
- 23
-
1Just a note, in windows (**in general**), we should restart our IDE if the environment variable was created **after** the IDE is opened. – Richard Lee Oct 28 '17 at 12:12
-
@RichardLee what? – MarcioB Nov 09 '17 at 10:02
-
Hi @MarcioB, so, i'm using IntelliJ and i had to restart it, but it does not appeared so obvious to me at first moment. Just for help someone with it. – Richard Lee Nov 12 '17 at 13:56
-
@RichardLee but those are environment variables inside a lambda in AWS, so why do you need to create them in your OS? – MarcioB Nov 13 '17 at 14:02
-
To do test locally – Richard Lee Nov 13 '17 at 17:34
-
2Will it give the same result with System.getProperty() ? – krackoder Nov 30 '17 at 21:56
-
You will need to setup SAM Local for this purpose and you can then easily set the environment variable in a separate file and provide at run time. – Kundan Ray Sep 13 '18 at 08:28
-
I also used same thing recently. – BishalG Aug 14 '20 at 06:53
4
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;

dassum
- 4,727
- 2
- 25
- 38
-
-
yes, you can, but only use bare minimum dependency to keep the jar size small. – dassum May 09 '20 at 05:46
-
In this case, if I want to use @Bean, what it's de minimun dependency that's have to use ? – EdwinCab May 09 '20 at 22:49
0
If you define environment variable in aws lambda with key as "MyKey" and value as "XYX" then I will use:
System.getenv("MyKey");

Akshay Khule
- 41
- 5