Skip to content

googleComputeInstanceGroup

Creates a group of dissimilar Compute Engine virtual machine instances. For more information, see the official documentation and API

-> Recreating an instance group that's in use by another resource will give a resourceInUseByAnotherResource error. You can avoid this error with a Terraform lifecycle block as outlined in the example below.

Example Usage - Empty instance group

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeInstanceGroup.ComputeInstanceGroup(this, "test", {
  description: "Terraform test instance group",
  name: "terraform-test",
  network: "${google_compute_network.default.id}",
  zone: "us-central1-a",
});

Example Usage - With instances and named ports

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeInstanceGroup.ComputeInstanceGroup(this, "webservers", {
  description: "Terraform test instance group",
  instances: [
    "${google_compute_instance.test.id}",
    "${google_compute_instance.test2.id}",
  ],
  name: "terraform-webservers",
  named_port: [
    {
      name: "http",
      port: "8080",
    },
    {
      name: "https",
      port: "8443",
    },
  ],
  zone: "us-central1-a",
});

Example Usage - Recreating an instance group in use

Recreating an instance group that's in use by another resource will give a resourceInUseByAnotherResource error. Use lifecycleCreateBeforeDestroy as shown in this example to avoid this type of error.

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeHttpsHealthCheckStagingHealth =
  new google.computeHttpsHealthCheck.ComputeHttpsHealthCheck(
    this,
    "staging_health",
    {
      name: "staging-health",
      request_path: "/health_check",
    }
  );
const dataGoogleComputeImageDebianImage =
  new google.dataGoogleComputeImage.DataGoogleComputeImage(
    this,
    "debian_image",
    {
      family: "debian-11",
      project: "debian-cloud",
    }
  );
const googleComputeInstanceStagingVm =
  new google.computeInstance.ComputeInstance(this, "staging_vm", {
    boot_disk: [
      {
        initialize_params: [
          {
            image: dataGoogleComputeImageDebianImage.selfLink,
          },
        ],
      },
    ],
    machine_type: "e2-medium",
    name: "staging-vm",
    network_interface: [
      {
        network: "default",
      },
    ],
    zone: "us-central1-c",
  });
const googleComputeInstanceGroupStagingGroup =
  new google.computeInstanceGroup.ComputeInstanceGroup(this, "staging_group", {
    instances: [googleComputeInstanceStagingVm.id],
    name: "staging-instance-group",
    named_port: [
      {
        name: "http",
        port: "8080",
      },
      {
        name: "https",
        port: "8443",
      },
    ],
    zone: "us-central1-c",
  });
googleComputeInstanceGroupStagingGroup.addOverride("lifecycle", [
  {
    create_before_destroy: true,
  },
]);
new google.computeBackendService.ComputeBackendService(
  this,
  "staging_service",
  {
    backend: [
      {
        group: googleComputeInstanceGroupStagingGroup.id,
      },
    ],
    health_checks: [googleComputeHttpsHealthCheckStagingHealth.id],
    name: "staging-service",
    port_name: "https",
    protocol: "HTTPS",
  }
);

Argument Reference

The following arguments are supported:

  • name - (Required) The name of the instance group. Must be 1-63 characters long and comply with RFC1035. Supported characters include lowercase letters, numbers, and hyphens.

  • zone - (Required) The zone that this instance group should be created in.


  • description - (Optional) An optional textual description of the instance group.

  • instances - (Optional) The list of instances in the group, in selfLink format. When adding instances they must all be in the same network and zone as the instance group.

  • namedPort - (Optional) The named port configuration. See the section below for details on configuration. Structure is documented below.

  • project - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

  • network - (Optional) The URL of the network the instance group is in. If this is different from the network where the instances are in, the creation fails. Defaults to the network where the instances are in (if neither network nor instances is specified, this field will be blank).

The namedPort block supports:

  • name - (Required) The name which the port will be mapped to.

  • port - (Required) The port number to map the name to.

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format projects/{{project}/zones/{{zone}}/instanceGroups/{{name}}

  • selfLink - The URI of the created resource.

  • size - The number of instances in the group.

Timeouts

This resource provides the following Timeouts configuration options: configuration options:

  • create - Default is 6Minutes
  • update - Default is 6Minutes
  • delete - Default is 6Minutes

Import

Instance group can be imported using the zone and name with an optional project, e.g.

$ terraform import google_compute_instance_group.webservers us-central1-a/terraform-webservers
$ terraform import google_compute_instance_group.webservers big-project/us-central1-a/terraform-webservers
$ terraform import google_compute_instance_group.webservers projects/big-project/zones/us-central1-a/instanceGroups/terraform-webservers