How to start a Spring Boot app with spring-cloud-starter-aws-jdbc dependency locally


Posted by Steven

To access a database on AWS, the following dependency is needed:

  1. implementation('org.springframework.cloud:spring-cloud-starter-aws-jdbc:2.2.4.RELEASE')

As soon it is added to a Spring Boot project, it can not be started locally because it will throw exceptions like this one:

  1. ...
  2. java.lang.IllegalStateException: There is no EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
  3. ...

The solution is to provide some dummy parameters and force Spring to not load some configurations by providing the local run configuration with the following parameters:

  1. cloud.aws.region.auto=false
  2. cloud.aws.region.static=us-east-1
  3. spring.autoconfigure.exclude=org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration

Alternatively, this can be added to application.yml:

  1. ###
  2. # AWS Settings
  3. #
  4. # Basically disable and mock AWS-related settings/code so that a local startup under profile "local" is possible.
  5. ###
  6. spring:
  7. profiles: local, junit, default
  8. autoconfigure:
  9. exclude: org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
  10. cloud:
  11. aws:
  12. region:
  13. auto: false
  14. static: us-east-1
  15. ---
Category: 
Share: