Configuring stateful metadata in MIGs Stay organized with collections Save and categorize content based on your preferences.
Instancemetadata is useful for setting properties for and communicating with your applicationsthrough the metadata server. For example, you can use metadata to configure theidentity of the virtual machine (VM) instance, environment variables,information about cluster architecture, or data range that a VM is responsiblefor.
By configuring stateful metadata in amanaged instance group (MIG),you ensure that instance-specific metadata is preserved onmanaged instanceautohealing, update, and recreate events.
Configure stateful metadata individually for VM instances in a MIG by setting itinper-instance configurations and applying the configuration. You can set a per-instance configuration on instancecreation or against existing managed instances. After the per-instance configuration isapplied, the MIG stores stateful metadata in thepreserved state from configuration (preservedStateFromConfig) field of a managed instance.
Before you begin
- Reviewwhen to use stateful MIGs andhow stateful MIGs work.
- If you haven't already, set upauthentication. Authentication verifies your identity 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:
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI 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.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.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.
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.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.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.
When you permanentlydeletean instance (either manually or by resizing), the MIG does not preservethe instance's stateful metadata.
Setting stateful metadata on instance creation
Set stateful metadata when manually creating instances in a MIG. This isuseful for migrating a stateful application on standalone VMs to a stateful MIGand when creating stateful instances.
When you manually create an instance in a MIG and supply stateful metadata, theMIG performs the following tasks:
- Creates amanaged instancefrom the instance template using the provided instance name.
- Creates a per-instance configuration with the provided stateful metadata andsets that metadata on the instance.
- Stores the stateful metadata in the preserved state from configuration(
preservedStateFromConfig) of the associated managed instance.
gcloud
To create a managed instance with a custom name and to set stateful metadataon that VM, use thegcloud compute instance-groups managed create-instance command with the--stateful-metadata flag.
gcloud compute instance-groups managed create-instanceNAME \ --instanceINSTANCE_NAME \ --stateful-metadataKEY=VALUE[,KEY=VALUE,...]
Replace the following:
NAME: the name of the MIG in whichto create an instanceINSTANCE_NAME: the name of theinstance to createKEYandVALUE: statefulmetadata key-value pairs to set individually for the instances inaddition to metadata defined in the instance template- The key values that you set here take priority over any conflictingkey values from the instance template
Example
You need to deploy a cluster of nodes,example-cluster, that can operatein one of two modes:active orstandby. You set the mode individuallyfor each VM in a cluster using metadata, for example:mode:active.You also configure how elaborate logging should be for each node, using alogging metadata key that can be set tobasic orelaborate. Theapplication on the node is configured using values from instance metadata.
To create an active node,node-12, with elaborate logging, you would runthe following command:
gcloud compute instance-groups managed create-instance example-cluster \ --instance node-12 \ --stateful-metadata mode=active,logging=elaborate
The command creates a VM,node-12, in theexample-cluster MIG andsets two metadata key-value pairs,mode:active andlogging:elaborate,for the new instance.
Terraform
To create a managed instance with a custom name and to set stateful metadataon that VM, use thegoogle_compute_per_instance_config resource.
The following sample uses a zonal MIG. If you don't have a zonal MIG yet,thencreate a zonal MIG with VMs confined to a single zone.
resource "google_compute_per_instance_config" "default" { instance_group_manager = google_compute_instance_group_manager.default.name zone = google_compute_instance_group_manager.default.zone name = "node-12" preserved_state { metadata = { mode = "active" logging = "elaborate" } }}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
REST
To create one or multiple managed instances in a MIG with custom VM namesand to set stateful metadata individually on these VMs, use one of thefollowing methods:
- For a zonal MIG, use the
instanceGroupManagers.createInstancesmethod. - For a regional MIG, use the
regionInstanceGroupManagers.createInstancesmethod.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/NAME/createInstances{ "instances": [ { "name": "INSTANCE_NAME", "preservedState" : { "metadata": { "KEY" : "VALUE", ... } } }, ... ]}Replace the following:
PROJECT_ID: the project ID for the requestZONE: thezone where the MIG is located(applies to a zonal MIG)- For a regional MIG, replace
zones/ZONEwithregions/REGIONand specify the region ofthe MIG
- For a regional MIG, replace
NAME: the name of the MIG in which to createan instanceINSTANCE_NAME: the name of theinstance to createKEYandVALUE: statefulmetadata key-value pairs to set individually for the instances inaddition to metadata defined in the instance template- The key values that you set here take priority over any conflictingkey values from the instance template
Example
You need to deploy a cluster of nodes,example-cluster, that can operatein one of two modes:active orstandby. You set the mode individuallyfor each VM in a cluster using metadata, for example:mode:active.You also configure how elaborate logging should be for each node, using alogging metadata key that can be set tobasic orelaborate. Theapplication on the node is configured using values from instance metadata.
To create an active node,node-12, with elaborate logging, execute thefollowing method:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-east1-c/instanceGroupManagers/example-cluster/createInstances{ "instances": [ { "name": "node-12", "preservedState" : { "metadata": { "mode":"active", "logging":"elaborate" } } } ]}The method creates a VM,node-12, in theexample-cluster MIG andsets two metadata key-value pairs,mode:active andlogging:elaborate,for the new instance.
Setting, modifying, and removing stateful metadata individually for existing VM instances
Set, modify, or remove stateful metadata for an existinginstance in a MIG by setting it in an associated per-instance configuration and thenapplying the configuration by updating the instance.
gcloud
To configure stateful metadata individually for a VM instance in a MIG, setor remove stateful metadata in the associated per-instance configuration. If youapply the configuration to the instance at the same time(--update-instance), you can choose whether to keep the instance running,restart it, or recreate it. If you don't apply the configuration(--no-update-instance), your changes don't take effect until yourecreate or update the instance.
If a per-instance configuration doesn't exist for a given instance, use thegcloud compute instance-groups managed instance-configs create commandwith one of the following flags:
gcloud compute instance-groups managed instance-configs createNAME \ --instanceINSTANCE_NAME \ --stateful-metadataKEY=VALUE[,KEY=VALUE,...] \ [--no-update-instance | --update-instance] \ [--instance-update-minimal-actionMINIMAL_ACTION]
If a per-instance configuration already exists for a given instance, use thegcloud compute instance-groups managed instance-configs updatecommand with:
- The
--stateful-metadataflag for setting or modifying metadata, or - The
--remove-stateful-metadataflag for removing instance-specificstateful metadata.
gcloud compute instance-groups managed instance-configs updateNAME \ --instanceINSTANCE_NAME \ [--stateful-metadataKEY=VALUE[,KEY=VALUE,...]] \ [--remove-stateful-metadataKEY[,KEY,...]] \ [--no-update-instance | --update-instance] \ [--instance-update-minimal-actionMINIMAL_ACTION]
Replace the following:
NAME: The name of the managedinstance group.INSTANCE_NAME: The name of theinstance for which to configure stateful metadata.KEY=VALUE: Stateful metadatakey-value pairs to set individually for the instance in addition to themetadata defined in the instance template. The key values that you sethere take priority over any conflicting key values from the instancetemplate.KEY: Instance-specific stateful metadata keys toremove from the per-instance configuration.- If a value for the key is not defined by the instance template,the key-value pair is removed completely from the instance when thechange is applied.
- If a value for the key is defined by the instance template, thevalue from the instance template is set on the instance when thechange is applied.
MINIMAL_ACTION: Perform at least the specifiedaction when applying the per-instance configuration update to the instance.AMINIMAL_ACTIONcan only be set when the--update-instanceflag is used. The value must be one of thefollowing:none: No action.refresh: Apply updates that are possible to apply withoutstopping the instance.restart: Stop the instance and then start it again.replace: Recreate the instance.
If omitted, the least disruptive action required by the update is used.
Example
You have a cluster of nodes,example-cluster, that can operate in one oftwo modes:active orstandby. You set the mode individually for eachVM in the cluster using metadata, for example:mode:active. You alsoconfigure how elaborate logging should be for each node, using aloggingmetadata key that can be set tobasic orelaborate. The application oneach node consumes the values from instance metadata.
The instance template definesmode:active andlogging:basic metadata tobe used as default for all instances. You have setlogging:elaborate in aper-instance configuration for thenode-12 VM in the cluster. Now, you want toswitchnode-12 to standby mode and switch logging tobasic for thisVM.
To switch thenode-12 instance to standby and its logging to basic, runthe following command:
gcloud compute instance-groups managed instance-configs update example-cluster \ --instance node-12 \ --stateful-metadata mode=standby \ --remove-stateful-metadata logging
The command does the following:
- Sets
mode:standbymetadata in the per-instance configuration that isassociated with the VM,node-12, in theexample-clusterMIG. - Removes
logging:elaboratemetadata from the per-instance configuration forthenode-12instance. - Applies the per-instance configuration change to the
node-12VM:- Sets
mode:standbymetadata, according to the configuration. - Sets
logging:basicmetadata from the instance template because thevalue for theloggingkey is no longer defined by the per-instanceconfiguration.
- Sets
- The change is applied to the VM immediately by default because theflag
--no-update-instanceis omitted. - The VM keeps running during the update because the
--instance-update-minimal-actionflag is omitted and the leastdisruptive action is chosen for the update by default, in this case:refresh.
REST
To configure stateful metadata individually for existing VM instances in aMIG, set or remove the metadata in the associated per-instance configurations.Then update the instance toapply the configuration.
If per-instance configurations don't yet exist for the given instances, usetheinstanceGroupManagers.updatePerInstanceConfigs methodwith stateful metadata:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/NAME/updatePerInstanceConfigs{ "perInstanceConfigs": [ { "name": "INSTANCE_NAME", "preservedState" : { "metadata": { "KEY": "VALUE", ... } }, "fingerprint: "FINGERPRINT" }, ... ]}updatePerInstanceConfigs methodto update existing per-instance configurations. In this case, the method fullyreplaces the specified per-instance configurations with the values you specify. Itis safer to use thepatchPerInstanceConfigs method to updateexisting per-instance configurations because patching keeps the omittedconfiguration unchanged and prevents the risk of accidental deletion ofstateful items or reset of values to their defaults.If per-instance configurations already exist for the given instances, use theinstanceGroupManagers.patchPerInstanceConfigs method
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/NAME/patchPerInstanceConfigs{ "perInstanceConfigs": [ { "name": "INSTANCE_NAME", "preservedState" : { "metadata": { "KEY": "VALUE", ... } }, "fingerprint: "FINGERPRINT" }, ... ]}Replace the following:
PROJECT_ID: The project ID for the request.ZONE: Thezone where the MIG is located(applies to a zonal MIG).- For a regional MIG, replace
zones/ZONEwithregions/REGIONand specify the region ofthe MIG.
- For a regional MIG, replace
NAME: The name of the MIG.INSTANCE_NAME: The name of theVM for which to configure stateful metadata.KEYandVALUE: Statefulmetadata key-value pairs to set individually for the instances inaddition to any metadata defined in the instance template.- Stateful metadata values defined for the keys that alreadyexist in the instance template override the values from theinstance template.
- Other metadata entries from the instance template remainunaffected and available.
- Providing
nullas a value removes the key from theper-instance configuration.
FINGERPRINT: (Optional). The fingerprint for thegiven configuration if it already exists. Used for optimistic locking.
TheupdatePerInstanceConfigs andpatchPerInstanceConfigs methods updatethe specified per-instance configurations but don't apply the configuration updates tothe associated VM instances. The changes are applied to a VM when youupdate or recreate the instance. To apply the changes to a VM, youcanapply the update manually or use the Updater in proactive or opportunistic mode.
Example
You have a cluster of nodes,example-cluster, that can operate in one oftwo modes:active orstandby. You set the mode individually for eachVM in the cluster using metadata, for example:mode:active. You alsoconfigure how elaborate logging should be for each node, using aloggingmetadata key that can be set tobasic orelaborate. The application oneach node consumes the values from instance metadata.
The instance template definesmode:active andlogging:basic metadata tobe used as default for all instances. You have setlogging:elaborate in aper-instance configuration for thenode-12 VM in the cluster. Now, you wantto switchnode-12 to standby mode and switch logging tobasic for thisinstance.
To switch thenode-12 VM to standby and its logging to basic, patchthe associated per-instance configuration by using thepatchPerInstanceConfigs method:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-east1-c/instanceGroupManagers/example-cluster/patchPerInstanceConfigs{ "perInstanceConfigs": [ { "name": "node-12", "preservedState" : { "metadata": { "mode": "standby", "logging": null } } } ]}The method does the following:
- Sets
mode:standbymetadata in the per-instance configuration associatedwith the VM,node-12, in theexample-clusterMIG. - Removes
logging:elaboratemetadata from the per-instance configurationbecause the supplied value isnull.
The configuration update is not yet applied to thenode-12 VM instance. Theconfiguration update will be applied when you next recreate or update the instanceor if you useproactive auto-updating.
To apply the per-instance configuration update to thenode-12 VM instance, calltheinstanceGroupManagers.applyUpdatesToInstances method for the instance:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-east1-c/instanceGroupManagers/example-cluster/applyUpdatesToInstances{ "instances": ["/zones/us-east1-c/instances/node-12"], "minimalAction": "NONE"}The method applies the updated per-instance configuration to thenode-12VM:
- Sets
mode:standbymetadata, according to the per-instance configuration. - Sets
logging:basicmetadata from the instance template becausethe value for theloggingkey is no longer defined by theper-instance configuration. - The VM keeps running during the update because
minimalActionisset toNONE, which allows the MIG to use the least disruptive actionrequired for the update. An instance metadata update requires theREFRESHaction, which does not disrupt a running instance.
Feedback
We want to learn about your use cases, challenges, and feedback about statefulMIGs. You can share your feedback with our team atmig-discuss@google.com.
What's next
- Learn more aboutStoring and retrieving instance metadata.
- Configure stateful persistent disksfor VMs in a MIG.
- Learn aboutapplying, viewing, and removingstateful configuration.
- Learn more aboutMIGsandworking with managed instances.
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-12-15 UTC.