Terraform for GCP How to create Auto Scaling for Compute Engine Instance

Paul Ravvich
Terraform for the Google Cloud Platform
4 min readMay 8, 2024

--

Hi, this is Paul, and welcome to the #34 part of my Terraform guide. Today we will discuss, how to create an Auto Scaling using the Terraform script.

Implementing Auto Scaling on Google Cloud Platform Using Terraform

Auto Scaling allows applications to efficiently manage workload fluctuations by automatically adjusting the number of compute resource instances. In this article, we will explore how to set up Auto Scaling on Google Cloud Platform (GCP) using Terraform.

Instance Template

The instance template is created using the google_compute_instance_template resource. This template defines the configuration for instances that will be created during scaling.

resource "google_compute_instance_template" "instance_sample" {
count = 1
name = "udemy-server-${count.index+1}"
description = "This is our autoscaling sample"
labels = {
environment = "sample"
name = "sample-server-${count.index+1}"
}
instance_description = "This is an instance that has been auto scaled"
machine_type = "n1-standard-1"
can_ip_forward = false
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
}
disk {
source_image = "ubuntu-os-cloud/ubuntu-1804-lts"
auto_delete = true
boot = true
}
disk {
auto_delete = false
disk_size_gb = 10
mode = "READ_WRITE"
type = "PERSISTENT"
}
network_interface {
network = "default"
}
metadata = {
foo = "bar"
}
service_account {
scopes = ["youre-email", "compute-ro", "storage-ro"]
}
}
  • count: The number of instance templates to create when executed.
  • name: The name of the instance template.
  • description: A description of the template to help identify its purpose.
  • labels: Labels assigned to the instance for categorization and filtering.
  • instance_description: A description of the specific instance.
  • machine_type: The type of machine, determining the computing resources available for each instance.
  • can_ip_forward: Allows or denies IP forwarding for the instance.
  • scheduling: Scheduler settings for the instance, including automatic restart and maintenance policy.
  • disk: Configuration of disks, including the operating system image and deletion rules. Supports multiple blocks for different disks.
  • network_interface: Network interfaces that define which network the instance will connect to.
  • metadata: Keys and values of metadata that can be used inside the instance.
  • service_account: Service accounts with access rights to various cloud services.

Health Check

Health checks allow monitoring of the state of instances in a group. If an instance fails a check, it can be restarted or replaced.

resource "google_compute_health_check" "health_sample" {
count = 1
name = "h-sample"
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 10
http_health_check {
request_path = "/health.jsp"
port = 8080
}
}
  • count: The number of health checks to create.
  • name: The name of the health check.
  • check_interval_sec: The frequency at which health checks are conducted.
  • timeout_sec: The timeout for waiting for a response from an instance before marking it as unhealthy.
  • healthy_threshold: The number of consecutive successful checks to consider the instance healthy.
  • unhealthy_threshold: The number of failed checks after which the instance is considered unhealthy.
  • http_health_check: Parameters for the HTTP check, including the path and port for sending requests.

Group Manager (Instance Group Manager)

The instance group manager manages the lifecycle of instances in a specified region using the provided template.

resource "google_compute_region_instance_group_manager" "sample_instance_group_manager" {
name = "instance-group-manager"
instance_template = "${google_compute_instance_template.instance_sample[0].self_link}"
base_instance_name = "sample-group-manager"
region = "europe-west2"
}
  • name: The name of the group manager.
  • instance_template: The link to the instance template used to create instances.
  • base_instance_name: The base name of instances created in the group.
  • region: The region where the instance group is managed.

Auto Scale Policy

The auto-scale policy defines how and when scaling should occur for the instance group.

 resource "google_compute_region_autoscaler" "sample_autoscaler" {
count = 1
name = "sample-autoscaler"
project = "sample-project-id"
region = "europe-west2"
target = "${google_compute_region_instance_group_manager.sample_instance_group_manager.self_link}"
autoscaling_policy {
max_replicas = 2
min_replicas = 1
cooldown_period = 60
cpu_utilization {
target = "0.8"
}
}
}
  • count: The number of auto-scaling policies.
  • name: The name of the auto scaler.
  • project: The project ID where the autoscaler is located.
  • region: The region where the auto scaler operates.
  • target: The scaling target, typically a link to the managed group.
  • autoscaling_policy: Parameters of the policy, including maximum and minimum number of instances, cooldown period after scaling changes, and CPU utilization targets.

Conclusion

Using Terraform to manage Auto Scaling on GCP allows for precise infrastructure management, ensuring high availability and efficient resource use. With the above configuration, you can automate the creation, monitoring, and scaling of your cloud resources, adapting them to the current needs of your applications.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Terraform for the Google Cloud Platform

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!