3

I'm creating a VM instance through cloud function in GCE.I want to add some environment variables to the Instance during creation. I'm referring this code for instance creation:

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/compute/api/create_instance.py

I don't want to add this in startup script, because already I'm running a set of tasks in startup script, and i want one of those tasks to use these environment variables. Is there any other way,like passing values in config, while creating instance ?

ezvine
  • 751
  • 1
  • 8
  • 23
  • Did you ever find a decent work around/solution? Not sure why this is so difficult to do. :-) – eddyizm Sep 03 '21 at 20:06
  • Its been so long, I don't remember how I managed to do that. I think I used a configuration file, which I copied to my machine during startup script. Then whichever program needed these variables, I read it from that file instead of using environment variables. @eddyizm – ezvine Dec 17 '21 at 14:28

2 Answers2

0

When you create a new Cloud-Function, you can expand the bottom menu named:

Environment variables, networking, timeouts and more

and set your environment variables from there.

enter image description here

Edit: I'd like to specify that environment variables are used by Cloud-Function itself on the main.py. However, you should may be interested into Instance Metadata & Metadata Server.

Bruno
  • 393
  • 2
  • 9
  • I dont want to add environment variable to the cloud function. I want to add environment variable, to the VM instance that i'm creating through cloud function. – ezvine Aug 28 '19 at 09:49
  • Don't seems to have an easy way to add environment variables to the instance level. However, I found this topic [1], DazWilkin proposed a workaround in comment. That may help you. -- [1]: https://stackoverflow.com/questions/54702164/setting-environment-variables-in-google-cloud-platform-using-cloud-functions?rq=1 – Bruno Aug 28 '19 at 14:53
  • Well I think a better work around for me would be using a configuration file, in the instance. – ezvine Sep 02 '19 at 18:51
-1

You can set environment variables by using gcloud command or through GCP Console [1]:

By using gcloud command: You can use the --set-env-vars flag to define a variable using the gcloud command-line.

e.g.:

gcloud functions deploy FUNCTION_NAME --set-env-vars env1=whatever,env2=whatever FLAGS...

*Note: The --set-env-vars and --env-vars-file flags for environment variables are destructive. That is, they replace all current variables with those provided at deployment. To make additive changes, use the --update-env-vars flag described in the next section.

e.g.:

gcloud functions deploy FUNCTION_NAME --update-env-vars env1=whatever

Through GCP Console:

  1. Open the Functions Overview page in the GCP Console:

    GO TO THE CLOUD FUNCTIONS OVERVIEW PAGE.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Expand the advanced settings by clicking More.

  5. In the Environment variables section, set variables by clicking Add variable.

References: [1] https://cloud.google.com/functions/docs/env-var#setting_environment_variables

Community
  • 1
  • 1
  • I dont want to add environment variable to the cloud function. I want to add environment variable, to the instance that i'm creating through cloud function. – ezvine Aug 28 '19 at 09:49
  • Use the following syntax to add an environment variable to your template: context.env["deployment"] – Victor_Torres Aug 28 '19 at 18:58
  • You can find more details about using environment variables here: https://cloud.google.com/deployment-manager/docs/configuration/templates/use-environment-variables#using_an_environment_variable – Victor_Torres Sep 02 '19 at 16:02