node.js - 使用 AWS codedeploy 部署 nodejs 实例的 NPM 问题

标签 node.js amazon-web-services amazon-ec2 npm aws-code-deploy

我目前正在尝试通过 Github 和 AWS Codedeploy 将 nodejs 应用程序自动部署到 EC2 实例。我已按照 here 中的说明进行操作尽可能接近,但我的 AfterInstall Hook 事件遇到了障碍。

这是我的 yml 文件:

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

我像这样通过 AWS CLI 调用部署:

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

一切正常,直到我进入 AfterInstall 阶段并执行我的“afterinstall.sh”。 该文件如下所示:

#!/bin/bash
cd /home/ec2-user/signal/app/
npm install

并产生以下错误日志,导致部署失败:

错误代码: ScriptFailed

消息:指定位置的脚本:backend/app/deploy/afterinstall.sh 以用户 ec2-user 身份运行失败,退出代码为 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

但是,如果我通过 ssh 进入我的 ec2 实例,请导航到临时目录:

/opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/

cd /home/ec2-user/signal/app/

然后手动运行 npm install,或通过 ./afterinstall.sh 运行我的脚本,然后 npm 运行正常。

为什么 Codedeploy 代理的情况有所不同?我正在使用 runas: ec2-user,所以我假设权限等与我作为 ec2-user 进入框时相同。

我做错了什么蠢事? 非常非常感谢。

最佳答案

正如 mbaird 和 Chris 在评论中准确指出的那样——我没有设置 PATH。所以 npm、node、pm2 和...都失败了。

通过实验,我似乎需要在 Codedeploy 部署过程的每一步中重新建立我的路径。所以在我的 stop.sh/beforeinstall.sh/afterinstall.sh/start.sh 的顶部,我包括:

source /home/ec2-user/.bash_profile

生活很美好。 然后我遇到了 pm2 没有在正确的工作目录中启动 Node 的其他问题,但是对 codedeploy 脚本的类似调整使它起作用了。

事后看来这一切都是显而易见的,但我非常感谢您的帮助。谢谢大家!

关于node.js - 使用 AWS codedeploy 部署 nodejs 实例的 NPM 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032751/

相关文章:

hadoop - Amazon Elastic MapReduce 在主节点上完成引导操作,但在核心节点上挂起

javascript - 从 module.exports 对象数组中检索值

node.js - AWS Lambda 上的 Microsoft 机器人 (botbuilder-aws-lambda) 错误

amazon-web-services - Redis Elasticache 缓存管理

amazon-web-services - AWS Cloudwatch 警报 Terraform

amazon-web-services - 我是 :PutRolePolicy User: xxx is not authorized to perform: iam:PutRolePolicy on resource: role yyy

node.js - 在云函数上创建消息主题

node.js - Hyperledger Fabric NodeJS REST API错误: stream removed

amazon-web-services - 试图将文件夹复制到 AWS EC2 实例 : Getting no directory found?

node.js - Elastic Beanstalk Nginx 服务静态文件