I am currently trying to automate deployment of a nodejs application to an EC2 instance via Github and AWS Codedeploy. I have followed the instructions from here as closely as possible, but I have hit a snag with my AfterInstall hook event.
Here is my yml file:
version: 0.0
os: linux
files:
- source: /backend
destination: /home/ec2-user/signal
permissions:
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
ApplicationStop:
- location: backend/app/deploy/stop.sh
timeout: 10
runas: ec2-user
BeforeInstall:
- location: backend/app/deploy/beforeinstall.sh
timeout: 1200
runas: ec2-user
AfterInstall:
- location: backend/app/deploy/afterinstall.sh
timeout: 1200
runas: ec2-user
ApplicationStart:
- location: backend/app/deploy/start.sh
timeout: 60
runas: ec2-user
ValidateService:
- location: backend/app/deploy/validate.sh
timeout: 60
runas: ec2-user
I invoke the deploy via the AWS CLI like so:
aws deploy create-deployment --application-name Signal --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name Production --description "Deployment" --github-location repository=githubusername/repository,commitId=ABCD123 --ignore-application-stop-failures
Everything works fine, until I reach the AfterInstall phase and my 'afterinstall.sh' is executed. That file looks like this:
#!/bin/bash
cd /home/ec2-user/signal/app/
npm install
And produces the following error log, causing a failed deployment:
Error Code: ScriptFailed
Message: Script at specified location: backend/app/deploy/afterinstall.sh run as user ec2-user failed with exit code 127
LifecycleEvent - AfterInstall
Script - backend/app/deploy/afterinstall.sh
[stderr]/opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/afterinstall.sh: line 7: npm: command not found
However, if I ssh into my ec2 instance, navigate to either the temp directory:
/opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/
or
cd /home/ec2-user/signal/app/
and either manually run npm install
, or run my script via ./afterinstall.sh
, then npm
runs fine.
Why are things different for the Codedeploy Agent? I'm using runas: ec2-user
, so I would assume permissions etc are the same as when I'm ssh'ed into the box as ec2-user
.
What idiotic thing am I doing wrong? Many, many thanks.
ec2-user
but not running your login scripts such as.bash_profile
and.bashrc
, so it doesn't have npm on the path. – Mark Bsource /path_to_bash_profile
on top of your afterinstall.sh – Chris