google-cloud-platformterraformterraform-provider-gcp

Creating two Google Cloud VPCs in different regions using the same Terraform main.tf file


I'm trying to create two VPCs (google_compute_network resources) in different regions with the same Terraform main.tf file. My google provider in provider.tf has a default region of us-east1.

I want to create a VPC in the default region (vpc1) and a VPC in europe-west1 (vpc2) in the same main.tf file. However, in the google_compute_network documentation there's not an argument for region so that I can override the default provider region. Is this possible or do I need to create separate main.tf and provider.tf files for each VPC?

Below are the provider.tf and main.tf I've tried to use. I'm new to GCP and Terraform so any help would be much appreciated.

main.tf

terraform {
    required_providers {
        google = {
            source = "hashicorp/google"
            version = "6.7.0"
        }
    }
}

//create vpcs
resource "google_compute_network" "vpc1" {
    name = "vpc1"
    auto_create_subnetworks = "false"
}
resource "google_compute_network" "vpc2" {
    name = "vpc2"
    auto_create_subnetworks = "false"
    region = "europe-west1"
}

provider.tf

provider "google" {
    project = "my-project"
    region  = "us-east1"
    zone    = "us-east1-a"
}

Solution

  • In GCP, VPC networks, including their associated routes and firewall rules, are global resources. They are not associated with any particular region or zone. Therefore, I'm not able to specify different regions for the VPCs.