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.
As accurately noted in the comments by mbaird and Chris - it was that I didn't have my PATH set. So npm, and node, and pm2 and... all failed.
Through experimentation, it appeared I needed to reestablish my path with every step of the Codedeploy deploy process. So at the top of my stop.sh/beforeinstall.sh/afterinstall.sh/start.sh, I included:
source /home/ec2-user/.bash_profile
and life was good. I then ran into other issues with pm2 not starting node in the right working directory, but similar tweaking to the codedeploy scripts got that working.
This was all obvious in hindsight, but I'm extremely grateful for the help. Thank you guys!
The host agent uses roots fairly stripped down environment. An exit code of 127 indicates that the OS can't find some file it needs to load the script (it could be the script of something that is needed to execute it).
The best thing to do is make sure npm is installed for root.
Since, the host agent sources /etc/profile when launched as a service, you can also add anything you need to get npm working there.
来源:https://stackoverflow.com/questions/34032751/npm-issue-deploying-a-nodejs-instance-using-aws-codedeploy