Google Compute Engine Autoscaler

This page shows how to write Terraform for Compute Engine Autoscaler and write them securely.

google_compute_autoscaler (Terraform)

The Autoscaler in Compute Engine can be configured in Terraform with the resource name google_compute_autoscaler. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

autoscaling.tf#L2
resource "google_compute_autoscaler" "lb" {
  name   = "ngx-plus-lb-autoscaler"
  zone   = var.region_zone
  target = google_compute_instance_group_manager.lb.self_link
  autoscaling_policy {
    max_replicas = 5
autoscaling.tf#L2
resource "google_compute_autoscaler" "lb" {
  name   = "ngx-plus-lb-autoscaler"
  zone   = var.region_zone
  target = google_compute_instance_group_manager.lb.self_link
  autoscaling_policy {
    max_replicas = 5
autoscaling.tf#L2
resource "google_compute_autoscaler" "lb" {
  name   = "ngx-plus-lb-autoscaler"
  zone   = var.region_zone
  target = google_compute_instance_group_manager.lb.self_link
  autoscaling_policy {
    max_replicas = 5
autoscaling.tf#L2
resource "google_compute_autoscaler" "lb" {
  name   = "ngx-plus-lb-autoscaler"
  zone   = var.region_zone
  target = google_compute_instance_group_manager.lb.self_link
  autoscaling_policy {
    max_replicas = 5
compute.tf#L151
resource "google_compute_autoscaler" "cr460-autoscaler" {
  name   = "cr460-autoscaler"
  zone   = "us-east1-c"
  target = google_compute_instance_group_manager.cr460-workload-gm.self_link

  autoscaling_policy {

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Creation timestamp in RFC3339 text format.

An optional description of this resource.

  • id optional computed - string
  • name required - string

Name of the resource. The name must be 1-63 characters long and match the regular expression 'a-z?' which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.

URL of the managed instance group that this autoscaler will scale.

  • zone optional computed - string

URL of the zone where the instance group resides.

  • autoscaling_policy list block

    The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. This prevents the autoscaler from collecting information when the instance is initializing, during which the collected usage would not be reliable. The default time autoscaler waits is 60 seconds. Virtual machine initialization times might vary because of numerous factors. We recommend that you test how long an instance may take to initialize. To do this, create an instance and time the startup process.

    The maximum number of instances that the autoscaler can scale up to. This is required when creating or updating an autoscaler. The maximum number of replicas should not be lower than minimal number of replicas.

    The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. If not provided, autoscaler will choose a default value depending on maximum number of instances allowed.

    Defines operating mode for this policy. Default value: "ON" Possible values: ["OFF", "ONLY_UP", "ON"]

    • cpu_utilization list block

      The target CPU utilization that the autoscaler should maintain. Must be a float value in the range (0, 1]. If not specified, the default is 0.6. If the CPU level is below the target utilization, the autoscaler scales down the number of instances until it reaches the minimum number of instances you specified or until the average CPU of your instances reaches the target utilization. If the average CPU is above the target utilization, the autoscaler scales up until it reaches the maximum number of instances you specified or until the average utilization reaches the target utilization.

    • load_balancing_utilization list block

      Fraction of backend capacity utilization (set in HTTP(s) load balancing configuration) that autoscaler should maintain. Must be a positive float value. If not defined, the default is 0.8.

    • metric list block

      The identifier (type) of the Stackdriver Monitoring metric. The metric cannot have negative values. The metric must have a value type of INT64 or DOUBLE.

      The target value of the metric that autoscaler should maintain. This must be a positive value. A utilization metric scales number of virtual machines handling requests to increase or decrease proportionally to the metric. For example, a good metric to use as a utilizationTarget is www.googleapis.com/compute/instance/network/received_bytes_count. The autoscaler will work to keep this value constant for each of the instances.

      Defines how target utilization value is expressed for a Stackdriver Monitoring metric. Possible values: ["GAUGE", "DELTA_PER_SECOND", "DELTA_PER_MINUTE"]

    • scale_in_control list block

      How long back autoscaling should look when computing recommendations to include directives regarding slower scale down, as described above.

      • max_scaled_in_replicas list block

        Specifies a fixed number of VM instances. This must be a positive integer.

        Specifies a percentage of instances between 0 to 100%, inclusive. For example, specify 80 for 80%.

  • timeouts single block

Explanation in Terraform Registry

Represents an Autoscaler resource. Autoscalers allow you to automatically scale virtual machine instances in managed instance groups according to an autoscaling policy that you define. To get more information about Autoscaler, see:

Tips: Best Practices for The Other Google Compute Engine Resources

In addition to the google_compute_disk, Google Compute Engine has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

google_compute_disk

Ensure the encryption key for your GCE disk is stored securely

It is better to store the encryption key for your GCE disk securely. Secret Manager could be used instead.

risk-label

google_compute_firewall

Ensure your VPC firewall blocks unwanted outbound traffic

It is better to block unwanted outbound traffic not to expose resources in the VPC to unwanted attacks.

risk-label

google_compute_instance

Ensure appropriate service account is assigned to your GCE instance

It is better to create a custom service account for the instance and assign it.

risk-label

google_compute_project_metadata

Ensure OS login for your GCE instances is enabled at project level

It is better to enable OS login for your GCE instances. Enabling OS login ensures that SSH keys used to connect to instances are mapped with IAM users, allowing centralized and automated SSH key management.

risk-label

google_compute_ssl_policy

Ensure to use modern TLS protocols

It's better to adopt TLS v1.2+ instead of outdated TLS protocols.

risk-label

google_compute_subnetwork

Ensure VPC flow logging is enabled

It is better to enable VPC flow logging. VPC flow logging allows us to audit traffic in your network.

Review your Google Compute Engine settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Frequently asked questions

What is Google Compute Engine Autoscaler?

Google Compute Engine Autoscaler is a resource for Compute Engine of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Compute Engine Autoscaler?

For Terraform, the satishbr/NGINX-Demos, nginxinc/NGINX-Demos and Singaravelanraja07/kkkkkkkkkkk source code examples are useful. See the Terraform Example section for further details.

security-icon

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.