Skip to content
Advertisement

Get environment variables in a cloud function

I have a Cloud Function in GCP that queries BigQuery in a specific project/environment. As I have multiple environments I would like to get the current project/environment of the cloud function. This is so that I can access BigQuery in the corresponding environment. Of course I could just hardcode the project_id, but I would like to do this programmatically.

According to Google environment variables are set automatically. But when I try to access those I cannot find any of them.

For instance I have tried the following, which gives me none of the env. var listed by Google.

print(os.environ)

Anyone managed to access environment variables at runtime?

Advertisement

Answer

Those environment variables you are referring to only applies to python 3.7, the second section on that page (https://cloud.google.com/functions/docs/env-var#nodejs_10_and_subsequent_runtimes) states :

All languages and runtimes other than those mentioned in the previous section will use this more limited set of predefined environment variables.

This means the GCP_PROJECT is no longer set with 3.8 (at least for now).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement