0
0
GCPcloud~10 mins

Multi-region architecture patterns in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your app needs to work fast and stay online even if one data center has problems. Multi-region architecture means running your app in several places around the world to keep it safe and quick for users everywhere.
When you want your website to load quickly for users in different continents.
When you need your app to keep working even if one cloud region has an outage.
When you want to follow data rules that require storing data in specific countries.
When you want to balance user traffic to avoid overloading one region.
When you want to reduce delays for users by placing servers closer to them.
Config File - main.tf
main.tf
provider "google" {
  project = "example-project"
  region  = "us-central1"
}

resource "google_compute_instance" "vm_us_central" {
  name         = "vm-us-central"
  machine_type = "e2-medium"
  zone         = "us-central1-a"

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

  network_interface {
    network = "default"
    access_config {}
  }
}

provider "google" {
  alias  = "europe"
  project = "example-project"
  region = "europe-west1"
}

resource "google_compute_instance" "vm_europe_west" {
  provider     = google.europe
  name         = "vm-europe-west"
  machine_type = "e2-medium"
  zone         = "europe-west1-b"

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

  network_interface {
    network = "default"
    access_config {}
  }
}

This Terraform file creates two virtual machines in two different Google Cloud regions: us-central1 and europe-west1. Each VM runs Debian Linux. The provider blocks set the regions. The resource blocks create the VMs in those regions. This setup is a simple example of multi-region deployment to improve availability and latency.

Commands
This command prepares Terraform to work with Google Cloud by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google... - Installing hashicorp/google v4.0.0... - Installed hashicorp/google v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command creates the virtual machines in both regions as defined in the configuration file. The -auto-approve flag skips manual confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_instance.vm_us_central: Creating... google_compute_instance.vm_europe_west: Creating... google_compute_instance.vm_us_central: Creation complete after 30s [id=projects/example-project/zones/us-central1-a/instances/vm-us-central] google_compute_instance.vm_europe_west: Creation complete after 35s [id=projects/example-project/zones/europe-west1-b/instances/vm-europe-west] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply step without asking for confirmation
This command lists all the virtual machines running in your Google Cloud project, showing that the VMs in both regions are active.
Terminal
gcloud compute instances list
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS vm-us-central us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING vm-europe-west europe-west1-b e2-medium 10.132.0.3 35.195.67.89 RUNNING
Key Concept

If you remember nothing else from this pattern, remember: running your app in multiple cloud regions helps keep it fast and available even if one region has problems.

Common Mistakes
Using the same provider block without alias for multiple regions
Terraform will only deploy resources to one region, ignoring others.
Use multiple provider blocks with aliases to specify different regions for resources.
Not verifying the created resources after deployment
You might think resources are created but they could have failed silently or partially.
Always run commands like 'gcloud compute instances list' to confirm resources are running.
Summary
Define multiple provider blocks with aliases to deploy resources in different regions.
Use 'terraform apply' to create resources in all specified regions.
Verify resource creation with 'gcloud compute instances list' to ensure multi-region deployment.