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?
4 Answers
you can get them with:
System.getenv("NAME_OF_YOUR_ENV_VARIABLE")
8 Comments
Richard Lee
Just a note, in windows (in general), we should restart our IDE if the environment variable was created after the IDE is opened.
MarcioB
@RichardLee what?
Richard Lee
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.
MarcioB
@RichardLee but those are environment variables inside a lambda in AWS, so why do you need to create them in your OS?
krackoder
Will it give the same result with System.getProperty() ?
|
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;
4 Comments
EdwinCab
In a lambda aws we can use spring ?
dassum
yes, you can, but only use bare minimum dependency to keep the jar size small.
EdwinCab
In this case, if I want to use @Bean, what it's de minimun dependency that's have to use ?
w.eric
sadly this does not work for me. i'm using spring-boot 3. Configuration Getter public class Config { Bean public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } Value("${SFTP_USERNAME:test_user}") private String username; };