Create a MIG with stateful disks Stay organized with collections Save and categorize content based on your preferences.
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
- Create an instance template, which is required in order to create a managed instance group.
- If you haven't already, then set up authentication.Authentication is the process by which your identity is verified for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Afterinstalling the Google Cloud CLI,initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update
.- Set a default region and zone.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Afterinstalling the Google Cloud CLI,initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
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 methodto
RECREATE
. - For stateful regional MIGs, you mustdisable proactive redistribution (set the redistribution type to
NONE
) 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:
- All permissions required to call the
instanceGroupManagers.insert
method (for zonal MIGs) - All permissions required to call the
regionInstanceGroupManagers.insert
method (for regional MIGs)
Console
Go to theInstance groups page.
The remaining steps appear in the Google Cloud console.
- Click
Create instance group . - Select the
New managed instance group (stateful) option. - Assign a name and optionally a description to your instance group.
- Choose an instance template for the instance group orcreate a new one.
- In the
Number of instances field, specify the initial number of VMs you need in this group. - In the
Stateful configuration section, underGroup config, click the disk you want to make stateful, then perform the following steps. - In theStateful section, selectYes.
- In theOn permanent instance deletion list, choose whether you want to detach the disk or delete the disk when you permanently delete the VM.
- To save the settings, clickDone.
- To create the MIG, click
Create .
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
- For a regional instance template:
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.
gcloud computeinstance-templates describe
command.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_manager
resource.
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, replace
zones/ZONE
withregions/REGION
and specify the region ofthe MIG.
- For a regional MIG, replace
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
- For a regional instance template:
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.
instanceTemplates.get
methodand reading theinstanceTemplates.disks[].deviceName
field inthe response.What's next
- Learn more aboutstateful MIGsand when to use them.
- Learn more aboutconfiguring stateful disks in MIGs.
- LearnHow stateful MIGs work.
- Learn more aboutconfiguring stateful MIGsfordisks,metadata,andIP addresses.
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.