gcloud compute


The gcloud command-line tool lets you manage your Compute Engine resources, using the gcloud compute command group. gcloud compute is an alternative to using the Compute Engine API.

The gcloud tool is part of the Cloud SDK and is a unified command-line tool that includes features like statement autocompletion, in-place updating, command-line help, human-readable and machine-parsable output formats, and integration with Cloud SDK.

If you have never used Compute Engine, get started with either the Linux quickstart or the Windows quickstart.

Before you begin

  • If you want to run gcloud compute in a command prompt, install, update, and initialize the Cloud SDK.
  • If you don't have a command prompt readily available, try using gcloud compute in Cloud Shell. Cloud Shell is automatically installed, updated, and initialized with the latest Cloud SDK.

Default properties

When you create a virtual machine instance, Compute Engine configures a VM using default properties from the metadata server. You can check your VM's default properties by running the following command:

gcloud compute project-info describe --project PROJECT_ID

Replace PROJECT_ID with your project ID.

Default project

If you set a default project when you setup and initialize the Cloud SDK, all gcloud compute commands use that project ID by default.

You can override the default project ID by using either of the following methods:

  • Run each gcloud compute command with the --project flag
  • Set the CLOUDSDK_CORE_PROJECT environment variable

    Linux and macOS

    Use the export command to set the project environment variable.

    export CLOUDSDK_CORE_PROJECT=PROJECT_ID
    

    Replace PROJECT_ID with the project ID of the project that you want to use as default.

    Unsetting project environment variables

    If you need to unset your project environment variables, use the following unset commands:

    unset CLOUDSDK_CORE_PROJECT
    

    Windows

    Use the set command to set the project environment variable.

    set CLOUDSDK_CORE_PROJECT=PROJECT_ID
    

    Replace PROJECT_ID with the project ID of the project that you want to use as default.

    Unsetting project environment variables

    If you need to unset your project environment variables, use the following command:

    set CLOUDSDK_CORE_PROJECT=
    

If you do not set a default project or environment variable, you must include a --project flag in each gcloud compute command that you run.

Default region and zone

Some gcloud compute commands require --region or --zone flags. Instead of specifying these flags each time you run commands, you can set a default region and zone for your project. If you don't set a default region or zone and you run a region- or zone-dependent command, the gcloud command-line tool might prompt you to provide a --region or --zone flag, or return an error.

You can set the default region and zone in three ways:

  • In the metadata server. The default region and zone set in the metadata server are applied to your local client when you run gcloud init.
  • In your local client. The default region and zone set in your local client override the default region and zone set in the metadata server.
  • In environment variables. The default region and zone set in environment variables override the default region and zone set in your local client and in the metadata server.

You can override the default region and zone in the metadata server, in your local client, or in environment variables, by providing --region and --zone flags when you run commands.

View default region and zone

To view your default region and zone, run the following commands:

gcloud config get-value compute/region
gcloud config get-value compute/zone

If the output is (unset), no default region or zone is set.

View available regions and zones

You can view a list of regions and zones by running the following commands:

gcloud compute regions list
gcloud compute zones list

Set default region and zone in the metadata server

The default region and zone in the metadata server apply to all configurations in your project. To set the default region and zone in the metadata server, do the following:

  1. Set the default region and zone:

    gcloud compute project-info add-metadata \
       --metadata google-compute-default-region=REGION,google-compute-default-zone=ZONE
    

    For example:

    gcloud compute project-info add-metadata \
       --metadata google