2

I'm following the guide here and can't seem to get my Python app (which is deployed fine on GCP) to read the environment variables I've created in Cloud Functions.

The REST endpoint for the function returns the environment variables fine as I've coded up the Python method in the function to just do os.environ.get() on a request parameter that is passed in. However, in my actual deployed application, I don't want to do a REST GET call every time I need an environment variable. I would expect using os.environ.get() in my application would be enough, but that returns blank.

How does one go about retrieving environment variables on GCP with just a simple os.environ.get() or do I really have to make a call to an endpoint every time?

Nightwolf
  • 4,495
  • 2
  • 21
  • 29
  • 1
    You can only get user-defined environment variables at runtime that you defined at deploy time for a given function. Per the page you cite, `gcloud functions deploy FUNCTION_NAME --set-env-vars FOO=bar,BAZ=boo` will permit you to `os.environ.get("FOO")` and `os.environ.get("BAR")` in `FUNCTION_NAME`. – DazWilkin Feb 15 '19 at 04:36
  • Thank you, but that doesn't solve my problem. I need to access os.environ.get("FOO") in another application deployed on GCP, not from inside the function. – Nightwolf Feb 15 '19 at 05:16
  • The only way for you to access one environment's variables (e.g. one function under Cloud Functions) from another environment (e.g. another function or some other GCP-deployed code) is through some form of inter-process comms and probably REST to access a Cloud Function. – DazWilkin Feb 15 '19 at 05:52
  • You can't just reach into another functions's environment. By definition, Cloud Functions environment variables are visible only to the the process where the function is running, That's just the way environment variables work. You'll need to have both bits of code consult some other source (maybe a database of some sort) for the config, which will complicate everything greatly. – Doug Stevenson Feb 15 '19 at 07:10
  • As mentioned, you can't just access the environment variables of Cloud Functions like that. I would go for an intermediate solution, like storing a JSON file on Cloud Storage with the par key-values of your environment variables, and access it from there in code with the GCS libraries. Not optimal, but works. – Mangu Feb 15 '19 at 09:43
  • 1
    I'm only using Cloud Functions because I thought this would be their use. So if I'm understanding this, if I have a Python application deployed on GCP, and I want to access an environment-specific variable (dependent on where the application is deployed), I have to have a different store accessed via REST or something similar? And that there's no way to just do `os.environ.get()` like we do "normally"? – Nightwolf Feb 17 '19 at 02:04

1 Answers1

0

I have been struggling with this for some time. The only solution I have found to set environment variables for the whole app is to define them in app.yaml. See the env_variables section here.

But then you cannot commit app.yaml to any version control repository if you don't want people to see the environment variables. You could add it to .gitignore. There are more secure ways to handle secrets storage if these variables contain sensitive data. If you need more robust security, you might find some inspiration here.

Andrew Puglionesi
  • 945
  • 1
  • 11
  • 16