11

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.

Sergei Basharov
  • 43,294
  • 56
  • 177
  • 295
Ali Parr
  • 4,587
  • 3
  • 27
  • 34
  • It's worth just highlighting because the error message is long. The eventual error is: npm: command not found – Ali Parr Dec 02 '15 at 00:24
  • 1
    I'm guessing it is running as `ec2-user` but not running your login scripts such as `.bash_profile` and `.bashrc`, so it doesn't have npm on the path. – Mark B Dec 02 '15 at 00:25
  • 1
    put `source /path_to_bash_profile` on top of your afterinstall.sh – Chris Dec 02 '15 at 10:13

2 Answers2

22

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!

Ali Parr
  • 4,587
  • 3
  • 27
  • 34
  • 1
    Would you share the tweaks you mention, because it looks like I'm searching for the same? – Galya Feb 26 '16 at 14:07
  • 3
    This is the correct answer. Be sure to remove #!/bin/bash and replace it with the source line. I banged my head against the wall on that one for an hour. – Noel Baron Aug 09 '16 at 15:54
  • 1
    For some reason this command didn't work for me. I added `export NVM_DIR="/home/ec2-user/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"` which worked – Kevindra Sep 30 '18 at 23:57
  • @NoelBaron, thanks mate, you saved me a lot of time for the future, was stuck on this for last 4 hours. – nishkaush May 28 '20 at 13:30
-4

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.

Jonathan Turpie
  • 1,247
  • 10
  • 15
  • 6
    Don't run node as root. – Noel Baron Aug 09 '16 at 15:30
  • @NoelBaron Care to elaborate? – beyondtdr Mar 15 '21 at 12:46
  • @beyondtdr This is a super old thread, but essentially you should always configure your systems to run node as a known user with specific privileges. In most AWS server resources you'll have something like `ec2-user` or `ubuntu`. You should run as these users on ports that are accessible to these users. Then use ELB's and networking rules to pipe privileged ports like 80/443 to your non-privileged port (3000). – Noel Baron Mar 15 '21 at 15:14