user2796352
user2796352

Reputation: 964

How to set up node.js process.env variables for production with an Amazon EC2 instance?

I have searched and searched for a straight forward answer to this question but I have found nothing. I have my local .env file and am successfully loading it with dotenv.

The only answer I can find for setting up the process.env variables for production is to use Elastic Beanstalk.

Is there a way to set these variables up with just an EC2 instance?

Upvotes: 2

Views: 4006

Answers (3)

Spandan Joshi
Spandan Joshi

Reputation: 902

if [[ ! -d "$MyVar" ]]; then 
    export MyVar="abc"
    echo 'export MyVar="abc"' >> ~/.bashrc
fi

use this code. it saves MyVar in ~/.bashrc permanently

Upvotes: 0

Jassi
Jassi

Reputation: 669

For Ubuntu based (Linux) EC2 Instances

  1. Edit ~/.bashrc file of User which will run the program

    sudo nano ~/.bashrc

  2. Add following line at the end, use any variable_name. Assuming unique and unused

    export API_KEY=btgj32fkf

  3. Save File and

    source ~/.bashrc

Now, in node program process.env.API_KEY will be available.

For different operating System of EC2, method above will slightly change

Upvotes: 2

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

It's a normal server. you can always set env by

EXPORT FOO=bar

Upvotes: 0

Related Questions