Search code examples
javaamazon-web-servicesspring-bootwaraws-codepipeline

Elastic Beanstalk | CodePipeline will not deploy .war file correctly


I'm having a problem with my .war file deployment through CodePipeline (using CodeBuild) to my Elastic Beanstalk environment. After a successful pipeline deployment, it throws a 404 error (see picture).

However, when I upload the .war file from my project code directly into my Elastic Beanstalk environment, it works just fine.

Ideally, I want to just be able to run the CodePipeline to update the versioning.

My CodeBuild (buildspec) -

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto11

  build:
    commands:
      - ls -la
      - cd prod/
      - mvn clean
      - mvn install
  # post_build:
  #   commands:
artifacts:
  files:
  - backend/target/backend-0.0.1-SNAPSHOT.war
  # name: artifact 
  base-directory: prod/

404 Error -

Error picture

I've tried this as well and it still gives me a 404 error

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto11

  build:
    commands:
      - ls -la
      - cd prod/
      - mvn clean
      - mvn install
post_build:
    commands:
      - mv backend/target/backend-0.0.1-SNAPSHOT.war ROOT.war
artifacts:
  files:
      - ROOT.war

Solution

  • AWS Elastic Beanstalk expects to find and run a ROOT.war at root directory.

    ROOT.war runs at myapp.elasticbeanstalk.com. In a single WAR source bundle, the application always runs at the root path. (Source)

    You have two options to resolve this

    1. Make sure that your artifact is named ROOT.war and is found at root level (in contrary to backend/target/backend-0.0.1-SNAPSHOT.war)
    2. Use the post_build phase in your CodePipeline to rename and move the file adequately (the easier option).

    Option 2 approach:

    post_build:
        commands:
          - mv backend/target/backend-0.0.1-SNAPSHOT.war ROOT.war