0
votes

I am creating a GCE VM with the gcloud compute instances create command and I am using the --metadata-from-files argument to pass in a startup shell script for some provisioning.

However, I need for a variety of environment variables to be pre-populated with specified values in order for the startup script to work. They can't be hard-coded into the startup script because they will be different for different users of the overall script I am writing (eg the env vars passed into the new instance might be based on the $USER invoking the script).

I tried adding key value pairs with the --metadata argument as well, but this does not work, and the referenced shell variables are still unbound in the execution of the startup script.

How can I issue a gcloud compute instances create command that forwards some set of environment variables that will be populated into the environment for the startup script to consume them?

The --metadata parameter seems like it should be correct since the docs say,

Metadata to be made available to the guest operating system running on the instances.

1

1 Answers

3
votes

You should be able to use this documentation to generate the appropriate curl commands to extract your metadata: https://cloud.google.com/compute/docs/storing-retrieving-metadata

You can then use the extracted values to set your environment variables.

(Edited to fix the URL.)