Terraform configuration for GCP updates based on either a fixed number or a percentage.

Murat Necip Arcan
3 min readJul 24, 2023

--

Terraform to manage resources on Google Cloud Platform (GCP). It defines an instance group manager with a rolling update policy based on a maximum surge value.

variable "fixed_value" {
description = "The value to use for fixed_value."
type = number
default = 4
}

variable "max_surge_value" {
description = "The value to use for max_surge. If use_fixed_max_surge is true, this is the fixed number; otherwise, it's the percentage."
type = number
default = 20
}

locals {
total_instances = google_compute_instance_group_manager.your-source-label.target_size
max_surge_instances = local.total_instances > 10 ? ceil(local.total_instances * (var.max_surge_value / 100)) : var.fixed_value
updated_instance_count = local.total_instances + local.max_surge_instances
}

resource "google_compute_instance_group_manager" "your-source-label" {
name = "my-instance-group"
zone = "us-central1-a"
base_instance_name = "my-instance"
target_size = 3

version {
instance_template = google_compute_instance_template.my_template.self_link
}

update_policy {
type = "ROLLING_UPDATE"
max_surge_fixed = local.total_instances > 10 ? null : local.max_surge_instances
max_surge_percent = local.total_instances > 10 ? "${var.max_surge_instances}%" : null
}
}

resource "google_compute_instance_template" "my_template" {
// ... your instance template configuration ...
}

Variables: The code defines two variables, “fixed_value” and “max_surge_value,” both of type number, with default values of 20. These variables allow customization of the instance group manager’s behavior.

Locals: The code then declares a “locals” block to calculate some intermediate values. It calculates the total number of instances from the target_size attribute of the instance group manager, and it computes the “max_surge_instances” based on the value of “max_surge_value” and “fixed_value.” If the total instances are greater than 10, “max_surge_instances” will be a percentage (max_surge_value is a percentage of total_instances), otherwise, it will be the fixed value.

Instance Group Manager: The resource “google_compute_instance_group_manager” defines the instance group manager configuration. It specifies the name, zone, and base instance name. The “target_size” is set to 3, but this can be changed based on requirements.

Instance Template: The code refers to an instance template resource “google_compute_instance_template” named “my_template,” which is expected to be defined elsewhere in the configuration. The details of this resource are not provided in the snippet, but it should include the configuration for the virtual machine instances to be created.

Update Policy: The instance group manager has a “ROLLING_UPDATE” policy. The “max_surge” attribute is set based on the value of “local.total_instances.” If the total instances are greater than 10, the “max_surge” will be “local.max_surge_instances + %,” indicating a percentage of the total instances that can surge during the update process. Otherwise, “max_surge” will be “local.max_surge_instances,” indicating a fixed number of instances that can surge during the update.

Overall, this Terraform configuration creates an instance group manager with a rolling update policy that allows for a surge in instances during updates based on either a fixed number or a percentage of the total instances. The instance template, not shown in the provided code, should contain the configuration details for the virtual machine instances created within the instance group.

Resources :

Automatically apply VM configuration updates in a MIG

https://cloud.google.com/compute/docs/instance-groups/rolling-out-updates-to-managed-instance-groups#using_a_fixed_number_or_a_percentage_in_update_requests

Terraform Google Compute Engine Instance Group Manager API

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance_group_manager

--

--