Mastodon

How to Start a Spring Boot App with spring-cloud-starter-aws-jdbc dependency locally

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

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:

...
 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
...

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:

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

Alternatively, this can be added to application.yml:

  ###
  #   AWS Settings
  #
  # Basically disable and mock AWS-related settings/code so that a local startup under profile "local" is possible.
  ###
spring:
  profiles: local, junit, default
  autoconfigure:
    exclude: org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
cloud:
  aws:
    region:
      auto: false
      static: us-east-1
---