title: JPA cannot connect to AWS RDS from Beanstalk but it works locally
body: <p>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.</p>
  <p>The connection works when running the app <strong>locally</strong>, in my machine, because I set the following routing configuration for the RDS server:</p>
  <p><a href="https://i.stack.imgur.com/dhlXq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dhlXq.png" alt="RDS routing" /></a></p>
  <p>As outlined, routings are also added for the security groups associated to my EC2 instances.</p>
  <p>Therefore, running <code>mysql</code> on the EC2 machine <strong>works</strong> and the database can be reached.</p>
  <p>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:</p>
  <p><a href="https://i.stack.imgur.com/F4ycg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F4ycg.png" alt="connection refused errors" /></a></p>
  <p>This doesn't seem to make any sense.
  The database is accessible from both the EC2 instance (verified via the <code>mysql</code> command) and outside AWS, so the only remaining cause would be having misconfigured the Spring Boot app properties.</p>
  <p>This doesn't seem to be the problem either because when running it locally, in my machine, the app has <strong>no issues</strong> connecting to the RDS instance and running normally using the production MySQL server.</p>
  <p>I have separate <code>application-development.properties</code> and <code>application-production.properties</code> files, but I set the relevant properties to the same values:</p>
  <pre><code>spring.datasource.url                           = jdbc:mysql://XXXXXX.rds.amazonaws.com:3306/ebdb?useSSL=false&amp;allowPublicKeyRetrieval=true&amp;useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC
  spring.datasource.username                      = XXXXXX
  spring.datasource.password                      = XXXXXX
  
  spring.datasource.driver-class-name             = com.mysql.cj.jdbc.Driver
  </code></pre>
  <p>Any pointers as to why my app could be running locally but not when deployed to Beanstalk?</p>