简体   繁体   中英

How do I specify different regions per resource for Terraform GCP module?

The documentation for the Terraform google provider module lists a global option to set a region:

region - (Optional) The region to operate under, if not specified by a given resource. This can also be specified using any of the following environment variables (listed in order of precedence):

GOOGLE_REGION

GCLOUD_REGION

CLOUDSDK_COMPUTE_REGION

However, I found no way to specify a region for a google_compute_instance or a google_compute_disk resource. How do I create multiple instances/disks in different regions within the same project?

OP's phrasing of the answer:

Both of these resource types are located within a single zone, they have a zone field accordingly to specify where to provision them. Since a zone is located in a single region, specifying the requested zone for the resource is enough because it implicitly specifies the region as well. There is no option to specify the region for these resource types because that would be redundant along with specifying the zone, and specifying only the region would not be enough.

Original answer provided:

Both of the resources you linked have the zone tag, which is where instances and VM disks need to be located, as they are not region-wide. Zones are located within a region, and usually there are two or three zones for each region.

For example, taking the region us-west1 , in this list you can see that it has the zones a , b and c , which when specified in the zone tag need to be written as us-west1-a , us-west1-b or us-west1-c .

Edit :

This example shows an example terraform configuration file, which creates two different Compute Engine VM instances in two different zones, located in two different regions:

provider "google" {
        project="YOUR-PROJECT"  # Project ID
        region="europe-west2"   # Default resource region 
        zone="europe-west2-b"   # Default resource zone 
}

/*
 * Create instance in region Europe West 1, zone b
 */
resource "google_compute_instance" "europe_instance"{
        name            = "europe-instance-1"
        machine_type    = "n1-standard-1"
        zone            = "europe-west1-b"

        boot_disk {
                initialize_params {
                        image = "debian-cloud/debian-9"
                }
        }
        network_interface {
                network = "default"
        }

}

/*
 * Create instance in US West 1, zone c
 */
resource "google_compute_instance" "us_instance"{
        name            = "us-instance-2"
        machine_type    = "n1-standard-1"
        zone            = "us-west1-c"

        boot_disk {
                initialize_params {
                        image = "debian-cloud/debian-9"
                }
        }

        network_interface {
                network = "default"
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM