简体   繁体   English

使用 pm2 无法访问 ElasticBeanstalk 环境变量

[英]ElasticBeanstalk Environment Variables not accessible using pm2

I have successfully been able to run my application on ElasticBeanstalk using the basic npm start script shown below.我已经能够使用如下所示的基本 npm 启动脚本在 ElasticBeanstalk 上成功运行我的应用程序。

"scripts": {
    "start": "NODE_ENV=production node ./bin/www"
}

On my server I have now installed pm2 globally and updated the start script in my package.json to use pm2.在我的服务器上,我现在已经全局安装了 pm2 并更新了我的package.json中的start脚本以使用 pm2。

"scripts": {
    "start": "pm2 start ./bin/www"
}

pm2 starts the process, I can see this from looking at my logs however it does not seem to have access to the environment variables I set for ElasticBeanstalk. pm2 启动该过程,我可以通过查看我的日志看到这一点,但它似乎无法访问我为 ElasticBeanstalk 设置的环境变量。 I set the environment variables with the aws:elasticbeanstalk:application:environment option in the file .ebextensions/options.config shown below.我在文件.ebextensions/options.config中使用aws:elasticbeanstalk:application:environment选项设置环境变量,如下所示。

option_settings:
  aws:elasticbeanstalk:application:environment:
    keyOne: foo
    keyTwo: bar
    keyThree: foo

pm2 complains about not being able to read a property of undefined for an environment variable. pm2 抱怨无法读取环境变量的未定义属性。

0|www    | TypeError: Cannot read property 'keyOne' of undefined
0|www    |     at Object.<anonymous> (/var/app/current/app.js:26:38)
0|www    |     at Module._compile (internal/modules/cjs/loader.js:999:30)
0|www    |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
0|www    |     at Module.load (internal/modules/cjs/loader.js:863:32)
0|www    |     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
0|www    |     at Module.require (internal/modules/cjs/loader.js:887:19)
0|www    |     at Module.Hook._require.Module.require (/var/app/current/node_modules/require-in-the-middle/index.js:80:39)
0|www    |     at require (internal/modules/cjs/helpers.js:74:18)
0|www    |     at Object.<anonymous> (/var/app/current/bin/www:7:11)
0|www    |     at Module._compile (internal/modules/cjs/loader.js:999:30)

Do I need to source a file first?我需要先获取文件吗? I would like to avoid setting environment variables in a ecosystem.config.js file for pm2 as I can also manage the environment variables at the moment in a visual way from the AWS console using the approach currently implemented.我想避免在 pm2 的生态系统.config.js 文件中设置环境变量,因为我现在还可以使用当前实施的方法从 AWS 控制台以可视方式管理环境变量。

If you already created the PM2 process and then updated environment variables, you should delete the PM2 process and create it again using ecosystem file.如果您已经创建了 PM2 流程,然后更新了环境变量,您应该删除 PM2 流程并使用生态系统文件重新创建它。

The delete and create actions consider newly created environment variables, but not the restart.删除和创建操作考虑新创建的环境变量,但不考虑重新启动。 I don't know why.我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM