Create a MIG with stateful disks


This document describes how to create amanaged instance group (MIG)that preserves the data on disks with a given device name for all of the MIG'sVMs, even in the event of VM recreation—for example when a VM in the MIG isautohealed, updated, or recreated. Preserving disks is useful for certainworkloads—for example, for databases or legacy applications.

In addition to preserving disks for all VMs in the group, you can also configurea stateful MIG for the following:

  • You can add and preserve instance-specific disks.
  • You can add and preserve instance-specific metadata.
  • You can add and preserve IP addresses.

For more information, see thestateful MIG overview.

You can also read about otherbasic scenarios for creating a MIG.

Before you begin

Limitations

A MIG withstateful configuration—astateful MIG—has the following limitations:

  • You cannot use autoscaling if your MIG has stateful configuration.
  • If you want to use automated rolling updates, you must set thereplacement methodtoRECREATE.
  • For stateful regional MIGs, you mustdisable proactive redistribution (set the redistribution type toNONE) to prevent deletion of statefulinstances by automatic cross-zone redistribution.
  • If you use anall-instances configurationto override instance template properties, you cannot specify those propertiesin any per-instance configuration and at the same time in the group'sall-instances configuration.

To see the full list of MIG limitations, which varies based on the configurationthat you use, seeMIG limitations.

Create a MIG with stateful disks

Use theGoogle Cloud console, thegcloud CLI,Terraform, orREST.

Pro Tip: Consider storing data on an additional persistent disk and keeping the boot disk, containing the application, stateless. Such a configuration makes your application resilient to boot disk file system corruption. This configuration also simplifies VM updates because the MIG can recreate boot disks based on the immutable source image that you specify in the MIG's instance template.

Permissions required for this task

To perform this task, you must have the followingpermissions:

Console

  1. Go to theInstance groups page.

    Go to Instance groups

    The remaining steps appear in the Google Cloud console.

  2. ClickCreate instance group.
  3. Select theNew managed instance group (stateful) option.
  4. Assign a name and optionally a description to your instance group.
  5. Choose an instance template for the instance group orcreate a new one.
  6. In theNumber of instances field, specify the initial number of VMs you need in this group.
  7. In theStateful configuration section, underGroup config, click the disk you want to make stateful, then perform the following steps.
    1. In theStateful section, selectYes.
    2. In theOn permanent instance deletion list, choose whether you want to detach the disk or delete the disk when you permanently delete the VM.
    3. To save the settings, clickDone.
  8. To create the MIG, clickCreate.

gcloud

To specify which disks from the instance template should be stateful on MIGcreation, use the--stateful-disk flag with thegcloud compute instance-groups managed create command:

gcloud compute instance-groups managed createINSTANCE_GROUP_NAME \    --templateINSTANCE_TEMPLATE_URL \    --sizeSIZE \    --stateful-disk device-name=DEVICE_NAME[,auto-delete=DELETE_RULE]

Replace the following:

  • INSTANCE_GROUP_NAME: The name of the managedinstance group to create.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:
    • For a regional instance template:projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
    • For a global instance template:INSTANCE_TEMPLATE_ID
  • SIZE: The initial number of VMs you needin this group.
  • DEVICE_NAME: The device name of adisk specified in the instance template.
  • DELETE_RULE: A value that prescribeswhat should happen to a stateful disk when a VM is deleted. Availableoptions are:

    • never: (Default.) Never delete the disk; instead, detach thedisk when its VM is deleted.
    • on-permanent-instance-deletion: Delete the disk whenits VM instance is permanentlydeleted from the instance group, forexample, when the managed instance is deleted manually or when thegroup size is decreased.

    Regardless of the value of the delete rule, stateful disks are alwayspreserved on VM autohealing, update, and recreation operations.

Note: You can see the device names of disks that aredefined in an instance template by running thegcloud computeinstance-templates describecommand.

Terraform

If you haven't already created an instance template, which specifies themachine type, boot disk image, network, and other VM properties that youwant for each VM in your MIG,create an instance template.

To specify which disks from the instance template should be stateful on MIGcreation, include thestateful_disk block. The following sample creates azonal MIG with stateful disks. For more information about the resource thatis used in the sample, seegoogle_compute_instance_group_managerresource.

resource "google_compute_instance_group_manager" "default" {  name               = "igm-stateful-disk-basic"  zone               = "us-central1-f"  base_instance_name = "instance"  target_size        = 1  version {    instance_template = google_compute_instance_template.default.id  }  stateful_disk {    device_name = "example-disk"    delete_rule = "NEVER"  }}

To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.

REST

To specify which disks from the instance template should be stateful on MIGcreation, include them in thestatefulPolicy field in your request body.For a zonal MIG, use theinstanceGroupManagers.insert methodor for a regional MIG, use theregionInstanceGroupManagers.insert method.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instanceGroupManagers{  "name": "NAME",  "versions": [    {      "instanceTemplate": "INSTANCE_TEMPLATE_URL"    }  ],  "targetSize":SIZE,  "statefulPolicy": {    "preservedState": {      "disks": {        "DEVICE_NAME": {"autoDelete": "DELETE_RULE" },        "DEVICE_NAME": {"autoDelete": "DELETE_RULE" }      }    }  }}

Replace the following:

  • PROJECT: The project ID for the request.
  • ZONE: Thezone where the MIG is located(applies to a zonal MIG).
    • For a regional MIG, replacezones/ZONE withregions/REGION and specify the region ofthe MIG.
  • NAME: The name of the MIG tocreate.
  • INSTANCE_TEMPLATE_URL: the URL of the instance template that you want touse to create VMs in the MIG. The URL can contain either theIDor name of the instance template. Specify one of the following values:
    • For a regional instance template:projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
    • For a global instance template:INSTANCE_TEMPLATE_ID
  • SIZE: The initial number of instances you needin this group.
  • DEVICE_NAME: The device name of adisk specified in the instance template.
  • DELETE_RULE: A value that prescribeswhat should happen to the stateful disk when the VM instance is deleted.The available options are:

    • never: (Default.) Never delete the disk; detach the disk whenthe VM is deleted.
    • on_permanent_instance_deletion: Delete the stateful disk whenits VM is permanentlydeleted from the instance group, for example, when the managed instance isdeleted manually or when the group size is decreased.

    Regardless of the value of the delete rule, stateful disks arealways preserved on instance autohealing, update, and recreationoperations.

Note: You can see the device names of disks that aredefined in an instance template by calling theinstanceTemplates.get methodand reading theinstanceTemplates.disks[].deviceName field inthe response.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-16 UTC.