LAB11_GCP_Autoscaling: Managed Instance Groups and Instance Templates

Autoscaling: Managed Instance Groups and Instance Templates

☁ GCP 🟢 Beginner Autoscaling MIG Instance Template

Scenario

A single Compute Engine VM is a single point of failure and cannot handle varying amounts of traffic. To solve this, you use a Managed Instance Group (MIG). You define what the VMs look like via an Instance Template, put them in a MIG to manage their lifecycle, and attach an Autoscaler to automatically add/remove VMs based on CPU utilization.

Your Objectives
  • Create a google_compute_instance_template defining an e2-micro Debian VM with a startup script to install Nginx.
  • Create a google_compute_region_instance_group_manager using the template, deployed across multiple zones in the region. Set the base instance name to web-server.
  • Create a google_compute_region_autoscaler attached to the MIG, aiming for a target CPU utilization of 60%, with a minimum of 2 and maximum of 5 instances.
  • Output the Instance Group Manager's self-link.

Additional Context

Regional vs Zonal MIGs: A Regional MIG distributes instances across multiple zones in the region, providing protection against a single zone failing. A Zonal MIG lives entirely in one zone. In Terraform, you use google_compute_region_instance_group_manager for Regional MIGs. The target_size is controlled by the Autoscaler, so you should not set it manually on the manager if an autoscaler is attached.

Create Before Destroy: Instance Templates are immutable. To apply changes to a template, Terraform must destroy the old one and create a new one. Because the MIG references the template, you must add lifecycle { create_before_destroy = true } to the template to prevent dependency cycles.

variables.tf

variable "project_id" { type = string }
variable "gcp_region" { type = string; default = "us-central1" }

main.tf

provider "google" {
  project = var.project_id
  region  = var.gcp_region
}

# 1. Instance Template: Immutable blueprint for the VMs
resource "google_compute_instance_template" "web" {
  name_prefix  = "web-template-"
  machine_type = "e2-micro"

  disk {
    source_image = "debian-cloud/debian-11"
    auto_delete  = true
    boot         = true
  }

  network_interface {
    network = "default"
    access_config {} # Public IP required to download packages 
  }

  metadata_startup_script = <<-EOF
    #!/bin/bash
    apt-get update
    apt-get install -y nginx
    echo "

Hello from the MIG: $(hostname)

" > /var/www/html/index.html EOF # MUST be true so Terraform creates the new template before destroying the old lifecycle { create_before_destroy = true } } # 2. Managed Instance Group (MIG): Manages the VMs based on the template resource "google_compute_region_instance_group_manager" "web_group" { name = "web-mig-${var.gcp_region}" region = var.gcp_region base_instance_name = "web-server" version { instance_template = google_compute_instance_template.web.self_link } # NOTE: Do NOT set `target_size` here if an autoscaler controls this MIG } # 3. Autoscaler: Controls the size of the MIG dynamically resource "google_compute_region_autoscaler" "web_autoscaler" { name = "web-autoscaler" region = var.gcp_region target = google_compute_region_instance_group_manager.web_group.id autoscaling_policy { min_replicas = 2 max_replicas = 5 # 60 second delay before reading CPU from new instances cooldown_period = 60 cpu_utilization { target = 0.6 # Target 60% CPU utilization } } }

outputs.tf

output "mig_self_link" {
  value = google_compute_region_instance_group_manager.web_group.self_link
}

Workflow Commands

terraform init
terraform apply -var="project_id=my-gcp-project-123"

# List instances created by the MIG in the region
gcloud compute instances list --filter="name~'web-server.*'"

# View Autoscaler status
gcloud compute autoscalers describe web-autoscaler --region=us-central1

terraform destroy -var="project_id=my-gcp-project-123"
✓ Expected Output After Apply
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

mig_self_link = "https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1/instanceGroupManagers/web-mig-us-central1"