Can I automate Google Cloud SDK gcloud init - interactive command

cherba picture cherba · Feb 22, 2017 · Viewed 8.8k times · Source

Documentation on Google Cloud SDK https://cloud.google.com/sdk/docs/ directs one to run gcloud init after installing it.

Is there a way to automate this step given that gcloud init is an interactive command?

Answer

cherba picture cherba · Feb 22, 2017

One does not need to run gcloud init. Main goal is to make sure credentials are configured and perhaps the project property is set. If you have service-account credentials, gcloud can be configured and ready to go via the following:

gcloud auth activate-service-account --key-file=credential_key.json
gcloud config set project my-project

For completeness gcloud init essentially runs the following steps:

  1. Select configuration (one of the following)
    • gcloud config configurations create my_configuration
    • gcloud config configurations activate my_configuration
  2. Set up credentials (one of the following)
    • (Interactive) gcloud auth login
    • gcloud config set account my_existing_credentials
    • gcloud auth activate-service-account
  3. Set project
    • gcloud config set project my_project
      • List of accessible projects for set credentials can be seen via gcloud projects list
  4. (Optional) Set default GCE zone (Compute API must be enabled)
    • gcloud config set compute/zone my_default_gce_zone
      • List of zones can be obtained via gcloud compute zones list
  5. (Optional) Set default GCE region (Compute API must be enabled)
    • gcloud config set compute/region my_default_gce_region
      • List of regions can be obtained via gcloud compute regions list
  6. (Optional) Create default config file for gsutil
    • gsutil config -n -o ~/.boto