Google Cloud Platform (GCP) Code Examples

Listing Regions and Zones

Using the Google Cloud SDK (gcloud CLI)

List all available regions:

gcloud compute regions list

List zones within a specific region (e.g., us-central1):

gcloud compute zones list --filter="region:(us-central1)"

Using the Google Cloud Client Library for Python

Python script to list all available regions:


    from google.cloud import compute_v1

    def list_regions(project):
        client = compute_v1.RegionsClient()
        response = client.list(project=project)
        for region in response:
            print(region.name)
    

Python script to list zones within a specific region:


    from google.cloud import compute_v1

    def list_zones(project, region):
        client = compute_v1.ZonesClient()
        response = client.list(project=project)
        for zone in response:
            if zone.region.split('/')[-1] == region:
                print(zone.name)