javaamazon-web-servicesspring-bootamazon-elastic-beanstalk

How to access Environment Variable from AWS in my spring boot application


I am working on application which is deployed on AWS. I have set database configuration (URL, UserName, Password) in AWS Environment properties.

AWS environment Variables

Now how can I access these variables in my spring boot application?

My application.properties file looks like:

spring.datasource.driver-class-name = <DRIVER>
spring.datasource.url = <URL>
spring.datasource.username = <USERNAME>
spring.datasource.password = <PASSWORD>

Note: Currently I am accessing database details from application.properties file


Solution

  • To use environment variables in spring boot application.properties you can use the usual Spring placeholder notation:

    spring.datasource.url = ${JDBC_CONNECTION:default_value_connection}
    

    Further explanation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-placeholders-in-properties

    You can set JDBC_CONNECTION value in AWS Elastic beanstalk. If JDBC_CONNECTION environment variable not set it will use the 'default_value_connection'.