title: NPM issue deploying a nodejs instance using AWS codedeploy
body: <p>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  <a href="http://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html" rel="noreferrer">here</a> as closely as possible, but I have hit a snag with my AfterInstall hook event.</p>
  
  <p>Here is my yml file:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I invoke the deploy via the AWS CLI like so:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Everything works fine, until I reach the AfterInstall phase and my 'afterinstall.sh' is executed.
  That file looks like this:</p>
  
  <pre><code>#!/bin/bash
  cd /home/ec2-user/signal/app/
  npm install
  </code></pre>
  
  <p>And produces the following error log, causing a failed deployment:</p>
  
  <p><strong>Error Code:</strong> ScriptFailed</p>
  
  <p><strong>Message:</strong> Script at specified location: backend/app/deploy/afterinstall.sh run as user ec2-user failed with exit code 127</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However, if I ssh into my ec2 instance, navigate to either the temp directory:</p>
  
  <pre><code>/opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/
  </code></pre>
  
  <p>or </p>
  
  <pre><code>cd /home/ec2-user/signal/app/
  </code></pre>
  
  <p>and either manually run <code>npm install</code>, or run my script via <code>./afterinstall.sh</code>, then <code>npm</code> runs fine.</p>
  
  <p>Why are things different for the Codedeploy Agent? I'm using <code>runas: ec2-user</code>, so I would assume permissions etc are the same as when I'm ssh'ed into the box as <code>ec2-user</code>.</p>
  
  <p>What idiotic thing am I doing wrong?
  Many, many thanks.</p>