NPM issue deploying an instance of nodejs using AWS codedeploy

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.

+7
source share
2 answers

As accurately noted in the comments by mbaird and Chris, it was that I did not have a PATH set. So, npm and node, and pm2 and ... all failed.

Through experimentation, it turned out that I needed to restore my path with every step of the Codedeploy deployment 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. Then I ran into other problems when pm2 did not start node in the correct working directory, but a similar setup for codedeploy scripts made it work.

It was very obvious, but I am very grateful for the help. Thanks guys!

+15
source

The host agent uses the roots completely separated by the environment. The exit code from 127 indicates that the OS cannot find the file that it needs to load the script (this may be the script of what is needed to execute it).

The best thing to do is make sure npm is installed for root.

Since the start sources / etc / profile, running as a service, you can also add everything you need to make it work on npm.

-3
source

Source: https://habr.com/ru/post/1237208/


All Articles