I'm currently trying to automate the deployment of a nodejs application in an EC2 instance through Github and AWS Codedeploy. I followed the instructions from here as closely as possible, but came across a trap with my AfterInstall trap 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 deployment through the AWS CLI as follows:
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 complete. This file looks like this:
#!/bin/bash cd /home/ec2-user/signal/app/ npm install
And it creates the following error log, causing a failed deployment:
Error Code: ScriptFailed
Message: Script at the specified location: backend / app / deploy / afterinstall.sh starts as user ec2-user is not executed 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 go to my ec2 instance by ssh, go to 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 through ./afterinstall.sh , then npm works fine.
Why is the Codedeploy agent different? I am using runas: ec2-user , so I would assume that access permissions etc. Same as when I ssh'ed in a field like ec2-user .
What idiotic thing am I doing wrong? Great thank you.