Geoff
Geoff

Reputation: 71

Environment Variables in App Engine for local dev

What's the best way to set environment variables when developing nodejs on a local machine for App Engine Flex environment? If they're set in app.yaml then they don't get set during local development. Is there a way to force this, or should I use something like dotenv and keep track of the same environment variables in 2 places?

Upvotes: 6

Views: 4351

Answers (3)

prout.james
prout.james

Reputation: 109

Aidan's suggestion is good.

As configurations should be different on GAE and local, I would suggest option 2 is best - a separate .ENV for local and .YAML for GAE environments.

One minor point though. I'd suggest to add a .gcloudignore files, something like the below:

.gcloudignore
.git
.gitignore
.env
staging.yaml
node_modules/

Upvotes: 0

Gunar Gessner
Gunar Gessner

Reputation: 2651

Sensitive data (e.g. API Keys) should not be committed to source code.

The way I went around that is storing a .env file in Google Storage. Then you can use @google-cloud/storage to download it in production (using a prestart hook) and dotenv to load variables into memory.

You may find a full guide here: http://gunargessner.com/gcloud-env-vars/


PS: I would go for Aidan's answer for storing any data that is not sensitive. I have myself used dotenv satisfactorily in the past. Similar to that, there's nconf, the package that gcloud itself uses for examples. Pretty neat!

Upvotes: 4

Aidan Hoolachan
Aidan Hoolachan

Reputation: 2379

Option 1:

require('dotenv').config({path: '/custom/project/root/app.yaml'})

Option 2:

Maintain both a .env file and a .yaml file with the same keys but different values (local and GAE, respectively). In app.yaml, I make sure not to deploy my .env file by adding the following line:

skip_files : .env

You will then need to add a check on ('dotenv').config() to make sure that it doesn't error or overwrite your process variables if no .env file is detected.

Upvotes: 1

Related Questions