12

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.

3
  • It's worth just highlighting because the error message is long. The eventual error is: npm: command not found Commented Dec 2, 2015 at 0: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. Commented Dec 2, 2015 at 0:25
  • 1
    put source /path_to_bash_profile on top of your afterinstall.sh Commented Dec 2, 2015 at 10:13

3 Answers 3

32

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!

Sign up to request clarification or add additional context in comments.

5 Comments

Would you share the tweaks you mention, because it looks like I'm searching for the same?
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.
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
@NoelBaron, thanks mate, you saved me a lot of time for the future, was stuck on this for last 4 hours.
Thanks @Kevindra, this worked for me as well! Note that these are two lines: export NVM_DIR="/home/ec2-user/.nvm" and [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh".
0

I had the same issue because I was installed nvm first and then installed node using nvm install node. I was not able to reestablish my path as @AliParr mentioned in his answer. So I created a new ec2 instance and didn't install nvm. Instead I installed node and npm like so:

sudo apt update
sudo apt install nodejs npm

Now codedeploy had no problems with npm command.

Comments

-5

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.

3 Comments

Don't run node as root.
@NoelBaron Care to elaborate?
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.