How to set environment variable in EC2 Instance for a node app

Viewed 729

I'm currently running a node application on an EC2 Instance Linux using PM2 to manage and it keeps crashing, throwing an error that reads Username is required.

This username is being fed from process.env.USERNAME that I set using dotenv for development.

So far using AWS shell I have tried to pass the env variable in the following paths:

/etc/profile
/etc/bashrc
/etc/profile.d
~/.bash_profile
~/.bashrc

The variables were put in this form: export VARIABLE_KEY=VALUE

I also tried to put the variables inside /etc/environment. None of which is making it work.

I restarted the server each time, logged out, and logged back in.

1 Answers

The way I solved it was to restart pm2 using pm2 restart --update-env. When you first run pm2 start, it caches the env variables, so I needed to restart it to add the newly created env variables.

Related