Node.js 应用程序无法部署到 AWS Elastic BeanStalk 上可能是因为缺少 PostgreSQL 安装

Node.js app won't deploy on AWS Elastic BeanStalk possibly due to missing PostgreSQL install

我们有以下 package.json 文件:

{
  "name" : "DecisionsConsole",
  "version" : "0.0.0",
  "private" : true,
  "scripts" : {
    "start" : "node app.js"
  },
  "dependencies" : {
    "express" : "4.0.0",
    "cookie-parser" : "1.1.0",
    "serve-favicon" : "2.0.0",
    "morgan" : "1.1.1",
    "body-parser" : "1.3.0",
    "express-session" : "1.2.1",
    "errorhandler" : "1.0.1",
    "ejs" : "1.0.0",
    "request" : "2.34.0",
    "async" : "0.4.0",
    "lodash" : "2.4.1",
    "socket.io" : "1.0.4",
    "moment" : "~2.5.1",
    "pg" : "2.11.1",
    "connect" : "2.14.3",
    "sql" : "0.37.0",
    "request-json" : "0.4.10",
    "simplesets" : "1.2.0",
    "grunt" : "0.4.4",
    "aws-sdk" : "2.0.0-rc.19",
    "webworker-threads" : "0.4.13",
    "indexof" : "*",
    "serve-index" : "1.6.1",
    "node-rest-client" : "0.8.0",
    "querystring" : "0.2.0",
    "xml2js" : "0.4.6",
    "msexcel-builder" : "0.0.2",
    "mime" : "1.3.4"
  },
  "devDependencies" : {
    "grunt-contrib-compress" : "~0.7.0",
    "grunt-contrib-compass" : "~0.7.2",
    "grunt-contrib-uglify" : "*",
    "nodeunit" : "*"
  }
}

我们使用的是基于 64 位 Amazon Linux 2014.02 v1.0.1 运行 Node.js 的 AWS Elastic BeanStalk 环境。它是较旧环境而不是最新版本之一的原因是因为我们的某些软件包与较新的 AWS 环境之间似乎存在冲突。

部署时出现错误 Update environment operation is complete, but with errors. For more information, see troubleshooting documentation.

经过进一步调查,我们在 eb-tools.log 中发现了这样的错误:

 /bin/sh: pg_config: command not found gyp: Call to 'pg_config --libdir' returned 
 exit status 127. gyp ERR! configure error gyp ERR! stack Error: `gyp` failed with 
 exit code: 1 gyp ERR! stack at ChildProcess.onCpExit (/opt/elasticbeanstalk/node-
 install/node-v0.10.26-linux-x64/lib/node_modules/npm/node_modules/node-gyp/lib/
 configure.js:337:16) gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:
 98:17) gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:
 797:12) gyp ERR! System Linux 3.4.73-64.112.amzn1.x86_64 gyp ERR! command "node" "/
 opt/elasticbeanstalk/node-install/node-v0.10.26-linux-x64/lib/node_modules/npm/
 node_modules/node-gyp/bin/node-gyp.js" "rebuild" gyp ERR! cwd /tmp/deployment/
 application/node_modules/pg gyp ERR! node -v v0.10.26 gyp ERR! node-gyp -v v0.12.2 
 gyp ERR! not ok > webworker-threads@0.4.13 install /tmp/deployment/application/
 node_modules/webworker-threads > node-gyp rebuild make: Entering directory `/tmp/
 deployment/application/node_modules/webworker-threads/build' CXX(target) Release/
 obj.target/WebWorkerThreads/src/WebWorkerThreads.o 

我知道它有点难读,但它在 eb-tools.log 文件中都在一行上。经过一些挖掘,我发现了这个: Error installing node-gyp on ubuntu。但是,我不认为它是相关的。

经过进一步研究,我发现了这个:https://github.com/brianc/node-postgres/issues/684

好像有道理。问题是——如何让 AWS Elastic BeanStalk 自动安装 PG?

说明你没有在elastic beanstalk实例上安装postgres

  1. 从项目的根目录创建一个 .ebextensions 文件夹。
  2. 创建一个包含 package.config 和以下

    的文件

    packages: yum: postgresql93-devel: []

  3. 然后提交您的更改
  4. 最后使用 eb deploy
  5. 部署到 elastic beanstalk

那你应该不错

查看此 link 了解更多 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html

我在 Elastic Beanstalk 中部署 Node+PostgreSQL 应用程序时遇到了各种问题。首先,我收到上述消息 更新环境操作已完成,但有错误。有关详细信息,请参阅故障排除文档, 所以我不得不稍微清理一下我的代码:在我的 IDE(AWS 自己的 Cloud9)中运行良好的东西在 EB 中却不起作用。例如,我在使用 bcrypt 时遇到问题,我不得不用 bcrypt.js 替换它。此外,我的 package.json 在依赖项中包含了 Node,并触发了错误。

得到一个像样的 "eb create" 后,最终的答案是 these instructions。花点时间跟随他们,因为通常的仓促试错不太可能成功。这是因为添加数据库增加了一些复杂性,主要是由于安全组设置。

Here 您将找到使用 PostgreSQL 数据库设置 Node.js 应用程序的完整演练(在之前的 link 中提到)。您有一个 github 存储库,您可以自己克隆和安装。

This video by AWS people 也有助于进一步了解 EB。

祝你好运!

接受的答案给出了直接的解决方案。那挺好的。但我强烈建议您 运行 您的 nodejs 应用程序作为 docker inside elasticbeanstalk。这样一来,应用程序 运行 在您部署的任何地方都以相同的方式运行 此外,您会及早发现构建问题并修复它。