JPA cannot connect to AWS RDS from Beanstalk but it works locally

Viewed 283

I'm deploying a Java 8 Spring Boot web app to AWS Elastic Beanstalk. I have an associated RDS MySQL instance and configured the relevant connection details.

The connection works when running the app locally, in my machine, because I set the following routing configuration for the RDS server:

RDS routing

As outlined, routings are also added for the security groups associated to my EC2 instances.

Therefore, running mysql on the EC2 machine works and the database can be reached.

The issue appears when deploying the app to Beanstalk, where it gets implemented into the EC2 instances. The app crashes because it gets connection refused errors when trying to connect to the MySQL RDS instance:

connection refused errors

This doesn't seem to make any sense. The database is accessible from both the EC2 instance (verified via the mysql command) and outside AWS, so the only remaining cause would be having misconfigured the Spring Boot app properties.

This doesn't seem to be the problem either because when running it locally, in my machine, the app has no issues connecting to the RDS instance and running normally using the production MySQL server.

I have separate application-development.properties and application-production.properties files, but I set the relevant properties to the same values:

spring.datasource.url                           = jdbc:mysql://XXXXXX.rds.amazonaws.com:3306/ebdb?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username                      = XXXXXX
spring.datasource.password                      = XXXXXX

spring.datasource.driver-class-name             = com.mysql.cj.jdbc.Driver

Any pointers as to why my app could be running locally but not when deployed to Beanstalk?

1 Answers

Recreating both the Beanstalk environment and RDS instance seemed to fix the issue.

Related