Search code examples
google-cloud-platformgcloud

List all offered machine types on Google Cloud


AWS CLI provides command describe-instance-types to list all offered EC2 instance types. It also allows to filter them by different attributes. Is it possible to do something similar with Google Cloud CLI?

I want to list all offered machine types with their attributes. Additionally, I would like to filter them by their attributes (memory size, cpus, etc).


Solution

  • Yes.

    https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list

    ZONE="us-west1-c" # For example
    gcloud compute machine-types list \
    --project=${PROJECT} \
    --filter=zone=${ZONE}
    

    Yields:

    NAME              ZONE        CPUS  MEMORY_GB  DEPRECATED
    c2-standard-16    us-west1-c  16    64.00
    c2-standard-30    us-west1-c  30    120.00
    c2-standard-4     us-west1-c  4     16.00
    c2-standard-60    us-west1-c  60    240.00
    c2-standard-8     us-west1-c  8     32.00
    

    NOTE ZONE is always us-west1-c

    And:

    gcloud compute machine-types list \
    --project=${PROJECT} \
    --filter=guestCpus=8
    

    Yields:

    NAME            ZONE                       CPUS  MEMORY_GB  DEPRECATED
    c2-standard-8   us-central1-a              8     32.00
    e2-highcpu-8    us-central1-a              8     8.00
    e2-highmem-8    us-central1-a              8     64.00
    e2-standard-8   us-central1-a              8     32.00
    n1-highcpu-8    us-central1-a              8     7.20
    

    NOTE CPUS is always 8

    You can append --format=json or --format=yaml to any gcloud command to have results presented using those formats.

    But for more details (and more filtering) you need to gcloud compute machine-types describe (rather than list):

    TYPE="n1-standard-1"
    gcloud compute machine-types describe ${TYPE} \
    --project=${PROJECT} \
    --zone=${ZONE}
    

    Yields:

    creationTimestamp: '1969-12-31T16:00:00.000-08:00'
    description: 1 vCPU, 3.75 GB RAM
    guestCpus: 1
    id: '3001'
    imageSpaceGb: 10
    isSharedCpu: false
    kind: compute#machineType
    maximumPersistentDisks: 128
    maximumPersistentDisksSizeGb: '263168'
    memoryMb: 3840
    name: n1-standard-1
    selfLink: https://www.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/machineTypes/n1-standard-1
    zone: us-west1-c
    

    Another way to grok Google's services is through APIs Explorer

    For example, Compute Engine's API is here

    This is useful in this case because it helps you understand the types that are returned by (every Google API method) including machineTypes.List.

    From this, you can determine what filtering you wish to employ.

    APIs Explorer also provides a way to interact with the underlying REST API directly and, it will generate e.g. curl commands for you.

    So, instead of gcloud compute machine-types list ..., you can:

    PROJECT=...
    ZONE=...
    
    TOKEN=$(gcloud auth print-access-token)
    
    # Compute Engine machineTypes.List()
    URL="https://compute.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/machineTypes"
    
    curl \
    --silent
    --header "Authorization: Bearer ${TOKEN}" \
    --header "Accept: application/json" \
    --compressed \
    "${URL}?filter=guestCpus%3D8"