By using AWS re:Post, you agree to the AWS re:Post Terms of Use

502 Bad Gateway on AWS Elastic Beanstalk with Java Spring Boot App and RDS database (Postgres)

0

I'm testing/learning how to upload and connect a Java Spring Boot app on AWS Elastic Beanstalk and connect it to a RDS Database. When I run the Spring app locally, connected to the aws server database (RDS), all is OK. BUT after I upload the Spring Boot app on AWS Elastic Beanstalk I get 502 Bad Gateway.

It's not the common: port 5000 problem. I set it in the application.properties.

I'm using a few dependencies in the pom.xml and I'm not sure, but could Lombok dependency create some problems? Or is something else?

Here are some of the nginx/error.log and the environement web.stdout logs (stack overflow limits the no of characters at 30k and I can't upload all) :

    ----------------------------------------
    /var/log/nginx/error.log
    ----------------------------------------
    2024/11/08 15:37:39 [error] 2183#2183: *6144 connect() failed (111: Connection refused) while connecting to upstream, client: 178.18.252.24, server: , request: "GET /apikeys.php HTTP/1.1", upstream: "http://127.0.0.1:5000/apikeys.php", host: "16.170.76.57"
    2024/11/08 15:37:39 [error] 2183#2183: *6144 connect() failed (111: Connection refused) while connecting to upstream, client: 178.18.252.24, server: , request: "GET /secrets.txt HTTP/1.1", upstream: "http://127.0.0.1:5000/secrets.txt", host: "16.170.76.57"
    2024/11/08 15:37:39 [error] 2183#2183: *6144 connect() failed (111: Connection refused) while connecting to upstream, client: 178.18.252.24, server: , request: "GET /secrets.php HTTP/1.1", upstream: "http://127.0.0.1:5000/secrets.php", host: "16.170.76.57"
    2024/11/08 15:37:39 [error] 2183#2183: *6144 connect() failed (111: Connection refused) while connecting to upstream, client: 178.18.252.24, server: , request: "GET /aws_credentials HTTP/1.1", upstream: "http://127.0.0.1:5000/aws_credentials", host: "16.170.76.57"
    2024/11/08 15:37:39 [error] 2183#2183: *6144 connect() failed (111: Connection refused) while connecting to upstream, client: 178.18.252.24, server: , request: "GET /aws_config HTTP/1.1", upstream: "http://127.0.0.1:5000/aws_config", host: "16.170.76.57"
---------------
----------------------------------------
    /var/log/web.stdout.log
    ----------------------------------------
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: 2024-11-08T18:54:59.164Z ERROR 167181 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: 2024-11-08T18:54:59.165Z  WARN 167181 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: 2024-11-08T18:54:59.170Z  INFO 167181 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: 2024-11-08T18:54:59.186Z  INFO 167181 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger :
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: 2024-11-08T18:54:59.210Z ERROR 167181 --- [           main] o.s.boot.SpringApplication               : Application run failed
    Nov  8 18:54:59 ip-172-31-25-182 web[167181]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
asked a month ago52 views
2 Answers
0

Hello.

Is it possible for you to share the contents of "application.properties"?
It's possible that the authentication information for the database is incorrect. Also, check to see if ElasticBeanstalk and RDS are deployed in different VPCs, making it impossible for them to communicate over the network.

profile picture
EXPERT
answered a month ago
  • spring.datasource.url=jdbc:postgresql://my-db.[redacted].eu-north-1.rds.amazonaws.com:5432/[db_name] This is in the app application.properties and it work on my machine. But when I upload the jar to elastic beanstalk and create an environement I get 502 Bad Gateway.

    Regarding the VPC I only have one and it seems that booths are on it. But maybe, since I'm new to AWS, I'm missing something. Maybe it's the VPC security groups. Honestly there seems that a lot of things can go wrong.

    Like I said on my machine the same Spring app connects OK with the RDS database. But when I set up de elastic beanstalk environment it gives 502 Bad Gateway.

    Thanks for your reply! I will also try a Docker approach and see how that works.

0

Based on the logs you've provided, it appears that your Spring Boot application is failing to start properly on AWS Elastic Beanstalk, which is causing the 502 Bad Gateway error. The main issue seems to be related to the database configuration.

The error message indicates that the application is unable to create the JPA EntityManagerFactory due to a problem with the JDBC configuration. Specifically, it's unable to determine the Dialect for your database connection.

This suggests that your application is not able to connect to your RDS PostgreSQL database. There could be several reasons for this:

  1. Database connection details: Ensure that your application.properties or application.yml file contains the correct database connection details (URL, username, password) for your RDS instance when deployed to Elastic Beanstalk.

  2. Security Group settings: Check if the security group associated with your Elastic Beanstalk environment allows inbound connections to your RDS instance.

  3. Environment variables: If you're using environment variables to store database credentials, make sure they are correctly set in your Elastic Beanstalk environment configuration.

  4. Database dialect: You might need to explicitly specify the Hibernate dialect for PostgreSQL in your application properties:

    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

  5. Database driver: Ensure that the PostgreSQL JDBC driver is included in your application's dependencies.

As for your question about Lombok, it's unlikely that Lombok is causing this particular issue. The problem appears to be specifically related to the database connection.

To resolve this, double-check your database configuration, ensure that your RDS instance is accessible from your Elastic Beanstalk environment, and verify that all necessary environment variables or configuration properties are correctly set. Once the database connection is properly established, your application should start successfully, and the 502 Bad Gateway error should be resolved.
Sources
QuickStart: Deploy a Java application to Elastic Beanstalk - AWS Elastic Beanstalk
ElasticBeanstalkClient (AWS SDK for Java - 2.21.29)
Configuring the proxy server - AWS Elastic Beanstalk
DescribeConfigurationSettingsRequest (AWS SDK for Java - 2.21.29)

profile picture
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions