Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 774
Geoff Avatar asked Jan 28 '17 15:01

Geoff


People also ask

How do I run App Engine locally?

Running your application locallySelect File > Open to open the project you want to run. Browse to the directory containing your project. Select Tools > Cloud Code > App Engine Run on a local App Engine Standard dev server.

What is App Engine standard environment?

The App Engine standard environment is based on container instances running on Google's infrastructure. Containers are preconfigured with one of several available runtimes. The standard environment makes it easy to build and deploy an application that runs reliably even under heavy load and with large amounts of data.


2 Answers

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!

like image 173
Gunar Gessner Avatar answered Oct 10 '22 14:10

Gunar Gessner


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.

like image 27
Aidan Hoolachan Avatar answered Oct 10 '22 13:10

Aidan Hoolachan