I'm trying to understand how to use environment variables properly for Google Cloud Functions.
I've deployed a function and I want to write and read it's environment variable (e.g.: XXX).
if not os.environ.get("XXX"):
print("XXX_SET_DEFAULT")
os.environ.setdefault("XXX","1")
else:
print("XXX_SET")
os.environ["XXX"] = "1"
print("XXX_GET", os.environ["XXX"])
After triggering the function, the value initially set in the Runtime environment variables list does not change.
Although the behavior is little bit different, because when I deploy the variable with the function with a dummy value I can see XXX_SET string in the logs, but when I don't deploy the variable first, for the first run I can see XXX_SET_DEFAULT, then for the second run there is XXX_SET.
I've set the variables with (and from the UI too):
gcloud functions deploy my_func \
...
--set-env-vars XXX=xxx
Questions:
- Is it possible to set this variable from the python code somehow?
- What makes this difference in the logs (
XXX_SET_DEFAULT, XXX_SETvs.XXX_SET, XXX_SET) - Why the value of this variable doesn't get set from python code?
- is it somehow related to the multiple instantiations of the function (when needed)?
- What is the difference between
Runtime environment variablesandBuild environment variables?- can I deploy the variables between the
Build environment variablessomehow?
- can I deploy the variables between the
- What are the best practices for using the Environment Variables with cloud functions at Google?