72

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 4

123

you can get them with:

System.getenv("NAME_OF_YOUR_ENV_VARIABLE")
Sign up to request clarification or add additional context in comments.

8 Comments

Just a note, in windows (in general), we should restart our IDE if the environment variable was created after the IDE is opened.
@RichardLee what?
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.
@RichardLee but those are environment variables inside a lambda in AWS, so why do you need to create them in your OS?
Will it give the same result with System.getProperty() ?
|
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;

4 Comments

In a lambda aws we can use spring ?
yes, you can, but only use bare minimum dependency to keep the jar size small.
In this case, if I want to use @Bean, what it's de minimun dependency that's have to use ?
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; };
0

I am using this -

System.getenv("VAR_NAME");

This works pretty well.

Comments

0

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

System.getenv("MyKey");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.