0

I'm building a simple hello world application in java (based on spring) which I launch to AWS through a pipeline.

The buildspec.yml is defined as follows:

version: 0.2

phases:
  install:
    runtime-versions:
      java: openjdk8
  build:
    commands:
      - mvn package

artifacts:
  files:
    - '**/*'

with the appspec.yml as follows:

version: 0.0
os: linux
files:
  - source: target/helloworld-1.0-SNAPSHOT.jar
    destination: /tmp

hooks:
  ApplicationStart:
    - location: codedeploy/ApplicationStart.sh
      timeout: 60
      runas: root

The file codedeploy/ApplicationStart.sh:

#!/usr/bin/env bash

JAR_FILE_HOME='/tmp/helloworld-1.0-SNAPSHOT.jar'

java -jar JAR_FILE_HOME

Weirdly enough the deployment fails with the following error:

Script at specified location: codedeploy/ApplicationStart.sh run as user root failed with exit code 127

Output log:

[stderr]/opt/codedeploy-agent/deployment-root/5092b759-ecc4-44cb-859a-9823734abc04/d-GVQ6R854B/deployment-archive/codedeploy/ApplicationStart.sh: line 9: java: command not found

This seems very counter-intuitive since I've installed java in the buildspec.yml. Do I need to install java manually again within the ApplicationStart script or am I doing something else wrong?

Aaron
  • 1,575
  • 10
  • 18
Mountain_sheep
  • 311
  • 2
  • 16

2 Answers2

0

CodeBuild doesn't have a link with your application instance instead it only create run time when it receives artifacts for build event.

You don't need to install JAVA run time every time with appspec.yml. I would recommend you to install JAVA run time on an EC2 instance then create an AMI, as a reference base image for future use or you can proceed with Elasticbeanstalk which has prebuilt environments.

matesio
  • 1,584
  • 17
  • 31
0

The other answer also suggests this but just to clarify:

  • CodeBuild (specified in buildspec.yml) does the artifact creation. Simply put it takes your code and creates the jar. You defined here the java version. However this has nothing to do with you instance where it will deployed. It is an image where the build happens.
  • CodeDeploy (specified in appspec.yml) is responsible for deploying the artifact on the instances defined and owned by you. If you created the target instance manually you need to make java available there. As matesio suggested above you could simplify / automate the instance creation with proper java env but that is your responsibility as that is your instance (not like the env used for the build which is configured by AWS in the background)