Nando
Nando

Reputation: 398

AWS CodePipeline successful, but not correctly deployed to Elastic Beanstalk

The (successful) deployment of my WAR file to Elastic Beanstalk gives me a 404 Not Found when I invoke the application URL. I can see a application.war file within /var/lib/tomcat8/webapps/ROOT/ instead of META-INF and WEB-INF, which is in there when I deploy manually.

When I pull the WAR file from S3 and deploy it to Elastic Beanstalk manually it works like a charm. Note: this is the same WAR file as generated by CodeBuild in my pipeline. Even better, if I secure copy (scp) the file to my local computer and upload it to Elastic Beanstalk it works as well.

It seems that everything works until the deployment, a working WAR file is even deployed to Elastic Beanstalk.

Going through eb-activity.log I can see it recognizes the WAR file and deploys it from a temporary directory to /var/lib/tomcat8/webapps/ROOT, but it isn't unpacked and the container/webserver isn't restarted.

How can I correctly deploy the WAR file with CodePipeline?

Upvotes: 1

Views: 2480

Answers (3)

RichMiller2012
RichMiller2012

Reputation: 31

I struggled with this for a while as well. Finally I was able to resolve it by having the buildspec.yml extract the WEB-INF directory from the built war file in the post_build section

Since AWS places a zip wrapper around your artifact it adds another folder level around what elasticbeanstalk actually needs

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto11
  build:
    commands:
       - mvn compile
  post_build:
    commands:
      - mvn package
      - mkdir artifact
      - unzip target/demo-0.0.1-SNAPSHOT.war -d artifact/
      - mv artifact/WEB-INF WEB-INF
artifacts:
  files:
   - WEB-INF/**/*
  name: artifact 

Upvotes: 0

joanlofe
joanlofe

Reputation: 3637

It seems that almost three years later AWS Codepipeline is not yet "WAR file deployment friendly". As pointed out in the comment by @Azeq the standard Elastic Beanstalk deployment procedure won't unzip the war file and nothing will be really deployed. CodePipeline reports success because the copy of the files is made without errors but Tomcat won't unzip the war file.

The solution is to provide your artifact in exploded form (already unzipped). To do so modify the post build phase and the artifact definiton of your CodeBuild buildspec.yml:

version: 0.2

phases:
    install:
        runtime-versions:
            java: openjdk8
    pre_build:
        commands:
            - echo CODEBUILD_RESOLVED_SOURCE_VERSION $CODEBUILD_RESOLVED_SOURCE_VERSION
    build:
        commands:
            - mvn compile
    post_build:
        commands:
            - mvn package
            - mkdir artifact    <-- create folder to extract war file content
            - unzip target/my.war -d artifact/    <-- unzip to that folder
artifacts:
    files:
        - artifact/**/*   <-- reference all those files as the artifact
    name: artifact
cache:
    paths:
        - '/root/.m2/**/*'

Note the mkdir and unzip commands in the post build phase, and how the files definition in the artifacts section is written. As per CodeBuild documentation, **/* means all files recursively.

Upvotes: 2

ankitAWS
ankitAWS

Reputation: 117

I tried to replicate the issue which you are facing. I think when creating the "war" file, you are putting the folder, which contains the "META-INF" and "WEB-INF" folders, as the root of the "war" output file.

Instead, you should put all the files (within the folder above) in the "war" file without the root-level folder.

Upvotes: 1

Related Questions