3

I want to use AWS CodeDeploy to deploy a jar file and then run my java -jar command from there once it is on the EC2. But I've noticed that AWS CodeDeploy only pulls zip, tar and tar.gz` from S3. I'm thinking I will use CLI from my local jenkins to push a .zip file (containing the jar) to S3, then run another CLI command to start AWS CodeDeploy which will pull the .zip from S3.

However I do have a question the details on AWS CodeDeploy:

Can I use the appspec.yml to issue two commands,

1) unzip the .zip from S3 once it is on the EC2

2) Issue the java -jar on a specific location?

Thanks

Billy
  • 1,049
  • 3
  • 14
  • 23

1 Answers1

5

1) You do not need issue an unzip command, the files section in appspec.yml is used to specify the files in your archive (source) you wish to copy to the file system on ec2 (destination) and will be done by the code deploy agent RE https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-files.html

2) Create a run script to issue java -jar command under the hook ApplicaitonStart RE https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html

Example appspec.yml on os linux based:

version: 0.0
os: linux
files:
  - source: ./
    destination: /home/ubuntu/myapp
hooks:
  ApplicationStart:
    - location: runapp.sh
      runas: ubuntu

runapp.sh:

#!/usr/bin/env bash
echo 'Starting my app'
cd '/home/ubuntu/myapp'
java -jar myapp.jar

In this example, you would include runapp.sh in your deployment package

Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
  • Getting `The CodeDeploy agent did not find an AppSpec file within the unpacked revision directory at revision-relative path "appspec.yml".` error. This is usually becasue the `appspec.yml` is not in the root so I've read, but here is my GIT: https://github.com/BillyCharter87/Tech-O-Dex-API which says otherwise. Upon firther inspection I noticed that my .zip file does not have my appspec.yml in it, only the jar. Is there a way to convert both .yml and .jar together into one? Also tried the 'From Github' option and got `MessageNo such file or directory - getcwd`? – Billy Aug 18 '18 at 02:52
  • You need to push the revision first, then create the deployment, https://docs.aws.amazon.com/codedeploy/latest/userguide/application-revisions-push.html – Luke Hutton Aug 18 '18 at 10:15
  • Sorry about the late responce, I got the code to deploy, but my I can't get it to runt he command to start my app: https://github.com/BillyCharter87/Tech-O-Dex-API/blob/1d18560e7daaa1494c713a09486a04e943a557ee/runapp.sh#L3 Is my cd command wrong? I think my app is running the `java -jar Tech-O-Dex-0.1.0.jar.` commands in the wrong folder but I don't know how to make sure it gets to `/home/ec2-user/Tech-O-Dex/target` Thanks – Billy Aug 21 '18 at 03:29
  • No problem, if your jar is in a folder called target in the zip deploy package, that should work, can you view the contents on s3 to verify? Lastly, check log files to see if any errors on startup `less /var/log/aws/codedeploy-agent/codedeploy-agent.log` – Luke Hutton Aug 21 '18 at 17:10
  • Looking at the /var/log/ command I see ` InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: runapp.sh run as user ec2-user failed with exit code 127 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:173:in `execute_script'` Looks like it is having error running to script, why I don't know though? The S3 bucket looks good, has everything I need in the .zip – Billy Aug 21 '18 at 21:29
  • Exit code 127 is returned by your shell /bin/bash when any given command within your bash script or on bash command line is not found in any of the paths, do you have java installed on the instance? – Luke Hutton Aug 21 '18 at 22:14
  • Yep, got Java on my machine. ` [ec2-user@ip-172-31-41-109 ~]$ java -version openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode) ` I can manually run `java -jar Tech-O-Dex-0.1.0.jar` on my machine under /home/ec2-user/Tech-O-Dex/target and my app will start fine. – Billy Aug 21 '18 at 22:19
  • ok, there should be more detail to deployment error under: `/opt/codedeploy-agent/deployment-root/xxx/yyy/logs/ ` where xxx is deployment id, can you check there for specifics – Luke Hutton Aug 21 '18 at 22:24
  • Ok, I'll check when I get back home. Thanks Luke! – Billy Aug 21 '18 at 22:27
  • Oh I see it `/usr/bin/env: sh: No such file or directory` Looking at this here: https://stackoverflow.com/questions/18172405/getting-error-usr-bin-env-sh-no-such-file-or-directory-when-running-command-p it says to run `dos2unix [filename]` but I'd have to run a new script on my current script to get it to run? I feel bad about all the questions, painfully new to linux :( – Billy Aug 22 '18 at 01:40
  • No that's ok, yes, you need to make sure you commit the bash script with unix line endings before you deploy, so just can do that on your computer, no need to run command on the host machine your deploying to – Luke Hutton Aug 22 '18 at 18:44
  • Ok got s successful deployment and application start, but it is getting hungup on `ApplicationStart` step, and will need it go get past `ValidateService` step, it just says pending and wont move? I've tried adding in a verify script: https://github.com/BillyCharter87/Tech-O-Dex-API/blob/master/appspec.yml but nothing? Any advice? Thanks a ton man. – Billy Aug 23 '18 at 00:43
  • No worries, sounds like perhaps the application itself is hung up? have you tried running it manually to see if runs ok? it could also be java app is blocking, you could utilize upstart script or run the jar in background, see https://stackoverflow.com/questions/12102270/run-java-jar-file-on-a-server-as-background-process for instance – Luke Hutton Aug 24 '18 at 17:15