Reputation: 630
I have successfully been able to run my application on ElasticBeanstalk using the basic npm start script shown below.
"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.
"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. I set the environment variables with the aws:elasticbeanstalk:application:environment option in the file .ebextensions/options.config
shown below.
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.
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.
Upvotes: 1
Views: 583
Reputation: 121
You can use the ecosystem.yml file... In package.json it worked for me in this way...
...
"dependencies": {
"pm2": "^5.3.1",
},
"scripts": {
"start": "./node_modules/pm2/bin/pm2 start ecosystem.yml --env production",
}
...
And in ecosystem.yml I have...
apps:
- script: ./webhook.js
name: "webhook-whatsapp"
env:
NODE_ENV: development
env_production:
NODE_ENV: production
And it Works on Elastic Beanstalk :D
Upvotes: 0
Reputation: 1
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.
The delete and create actions consider newly created environment variables, but not the restart. I don't know why.
Upvotes: -1