Home > Software design >  @GetMapping("/") does not work in Elastic Beanstalk but works locally
@GetMapping("/") does not work in Elastic Beanstalk but works locally

Time:11-09

My Elastic Beanstalk envrionemnt has a health status of severe. I believe this is due to the fact that I am not handling health checks. This can be seen in my logs:

/var/log/nginx/access.log
----------------------------------------
172.31.75.161 - - [08/Nov/2021:13:21:41  0000] "GET / HTTP/1.1" 404 116 "-" "ELB-HealthChecker/2.0" "-"
172.31.75.161 - - [08/Nov/2021:13:21:41  0000] "GET / HTTP/1.1" 404 116 "-" "ELB-HealthChecker/2.0" "-"
...

43 connect() failed (111: Connection refused) while connecting to upstream, client: 172.XX.XX.XX, server: , request: "GET / HTTP/1.1", upstream: "http://172.XX.XX.XX:8080/"

This is my Docker file:

# Build stage 
# 
FROM maven:3.8.1-jdk-8 
ADD src /tmp/src
ADD pom.xml /tmp/pom.xml
RUN mvn -f /tmp/pom.xml clean package

#
# Package stage
#
FROM openjdk:8
COPY --from=0 /tmp/target/para-host-0.0.1-SNAPSHOT.jar /usr/local/lib/para.jar
EXPOSE 5000
ENTRYPOINT ["java","-jar","/usr/local/lib/para.jar"]

So my Spring Boot app runs on port 5000. This works locally and in Elastic Beanstalk.

What I have tried so far I have tried using this dependency to add the health check functionality:

        <dependency>
            <groupId>
                org.springframework.boot
            </groupId>
            <artifactId>
                spring-boot-starter-actuator
            </artifactId>
        </dependency>

After that, I was able to run the server locally and hit the endpoint like this: http://localhost:5000/actuator/health and got the response:

{
    "status": "UP"
}

(Is this even a Status Code?)

This is where its broken In my load balancer in my Elastic Beanstalk environment, it says that my health check path is /. If I edit that value to /actuator/health/, then it simply reverts back to /. I have tried numerous times, even tried deleting it and creating a new one. Nothing. So I gave up on that. Instead, I figured, I would just make the path / return the status code for the health check.

So I made this:

    @GetMapping("/")
    public ResponseEntity<String> dummyMethod() {
        return new ResponseEntity<>("Hello from Dummy-Root-Method", HttpStatus.OK);
    }

thinking that now when the health check gets called, it will be successful, but this ALSO does not work. I am able to hit the / endpoint if I run the server locally, but when it is in Elastic Beanstalk, then it thinks the path does not exist. However, my other endpoints DO work. Here is a snippet of the working endpoint (getGroupResults) and then non-working endpoint (dummyMethod):

    @GetMapping("/get-group-results")
    public ResponseEntity<String> getGroupResults(@RequestBody ObjectNode objectNode)
            throws IOException, AddressException, MessagingException {
        String groupFolder = objectNode.get("group").asText();
        String sendToEmail = objectNode.get("email").asText();
        logger.info("Attempting to get results from folder: {}", groupFolder);
        String attachmentPath = Test.getMapFromGroupFolder(groupFolder).getAbsolutePath();
        String subject = "Results from parallenium";
        MailService.sendEmailWithAttachment(sendToEmail, attachmentPath, subject);
        return new ResponseEntity<>("Successfully sent results email", HttpStatus.OK);
    }

    /**
     * Dummy response for ELB so we do not receive errors
     * 
     * @return
     */
    @GetMapping("/")
    public ResponseEntity<String> dummyMethod() {
        return new ResponseEntity<>("Hello from Dummy-Root-Method", HttpStatus.OK);
    }

This is the response I get when trying to hit the /actuator/health/ endpoint in Elastic Beanstalk used like this http://para-server.us-east-1.elasticbeanstalk.com/actuator/health:

{
    "timestamp": "2021-11-08T16:56:20.785 00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/"
}

Again, this works locally. Why?

Summary. What can I do?

  1. Why can I not change the path to the health check in Elastic Beanstalk console?
  2. Why does the root path / work locally, but not in my Elastic Beanstalk environment?

I simply want my environment to not be in red. It is working fine, but has health check errors.

CodePudding user response:

Validate that Beanstalk is running the latest version of your code. May still be using a container spun up before you added the new mappings.

CodePudding user response:

The problem was that even though I was deploying a new version via the EBS CLI, it was not actually deploying my latest changes. If you have git setup in the project then it will only deploy the last commit. So I had to commit my changes and then re-deploy it and worked.

  • Related