Update instance properties

You can use a single request from the Google Cloud CLI or the Compute Engine API to update multiple instance properties and optionally restart the instance if necessary. The update method handles the logic for validating the updated instance properties and ensuring that the necessary resources are available to complete the update successfully. If the request includes invalid properties or if the requested resources are not available, the request returns an error and makes no changes to your instance. This protects the instance from partial updates and protects the instance from being stopped and unable to access resources to start up again.

To update the network interfaces (networkInterfaces) of an instance, see instead Update the network interfaces for an instance.

To add or remove Dynamic Network Interfaces from an instance, use the following procedures instead of updating the instance properties:

Before you begin

Restrictions

Permissions

You must have the compute.instances.update permission to update instances. This permission is included in several existing Identity and Access Management (IAM) roles. You must also have permission to use the resources that you want to modify on the instance. For example, to add a disk to an instance you must have the following permissions:

Best practices

To ensure that the instance update interface works most effectively for you, use the following best practices:

Properties that you can update

You can change only a specific set of instance properties through the update method. Updates to some properties require the instance to restart. To prevent accidental restarts on your instances, your request must define what action is allowed to be taken on your instances. Your request can specify one of the following actions:

The following properties require a restart (RESTART) to update:

The following properties only require a refresh (REFRESH) to update:

Update instance properties

Update your instances using the following steps:

  1. Export the existing instance configuration properties.
  2. Modify the properties of the instance configuration.
  3. Run a test request by specifying NO_EFFECT as the most disruptive action. The response indicates invalid fields in the instance configuration and indicates the action required to apply the changes.
  4. If the configuration is acceptable, request an instance update and specify the most disruptive action that is acceptable for your instance update.

The system starts the update process only if your request meets the following requirements:

If the requirements are met, the system starts the instance update process. Otherwise, the system makes no changes to your existing instance or its configuration.

You can initiate updates using either the gcloud CLI or the Compute Engine API.

gcloud

  • Use the instances export command to export the existing instance properties:

    gcloud compute instances export INSTANCE_NAME \
        --project PROJECT_ID \
        --zone ZONE \
        --destination=FILE_PATH
    

    Replace the following:

  • Use a text editor to modify one or more properties in the exported instance configuration file. To learn which properties you can update, see the list of updatable properties in this document. For example, to change the machine type of the instance, change the machineType property:

    ...
    machineType: https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/MACHINE_TYPE
    ...
    

    Replace the following:

    Tip: To avoid getting an error such as "ERROR: (gcloud.compute.instances.update-from-file) Cannot parse YAML: [Expected type for field value, found True (type )]", add quotes (' ') around any label values of yes or no in the exported instance configuration file. This indicates the values are strings, not Boolean values.
  • Use the instances update-from-file command to run a test update of the target instance. Specify the --most-disruptive-allowed-action=NO_EFFECT flag. The response identifies misconfigured properties and indicates whether a RESTART or REFRESH action is required to apply the update.

    gcloud compute instances update-from-file INSTANCE_NAME \
        --project PROJECT_ID \
        --zone ZONE \
        --source=FILE_PATH \
        --most-disruptive-allowed-action NO_EFFECT
    

    Replace the following:

  • Use the instances update-from-file command to update the target instance. If you are updating a property that requires the instance to restart, include the --most-disruptive-allowed-action=RESTART flag. If the properties don't require a restart, specify the --most-disruptive-allowed-action=REFRESH flag. To learn which properties require a restart, see the updatable properties list in this document.

    gcloud compute instances update-from-file INSTANCE_NAME \
        --project PROJECT_ID \
        --zone ZONE \
        --source=FILE_PATH \
        --most-disruptive-allowed-action ALLOWED_ACTION
    

    Replace the following:

  • If the update request is valid and the required resources are available, the instance update process begins. You can monitor the status of this operation by viewing the audit logs. If the update requires a restart and your command allows for a RESTART, the instance restarts to apply the changes.

    REST

    1. Use the instances.get method in the Compute Engine API to export the existing instance properties:

      GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
      

      Replace the following:

      • PROJECT_ID: the project ID for this request.
      • ZONE: the zone for this instance.
      • INSTANCE_NAME: the name for the instance you want to export.

      This request returns an instance resource definition.

    2. In the response, modify the instance resource properties. To learn which properties are updatable, see the updatable properties list in this document. For example, to change the machine type of the instance, change the machineType property. You can change multiple properties.

      {
        ...
        "machineType": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/MACHINE_TYPE",
        ...
      }
      

      Replace the following:

      • PROJECT_ID: the project ID for this request.
      • ZONE: the zone for this instance.
      • MACHINE_TYPE: the machine type of the instance, for example, n1-standard-2.
    3. Use the instances.update method in the Compute Engine API and specify the mostDisruptiveAllowedAction=NO_EFFECT query parameter to run a test update of the target instance. The response identifies misconfigured properties and indicates if a RESTART or REFRESH action is required to apply the update.

      PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME?mostDisruptiveAllowedAction=NO_EFFECT
      
      {
        ...
        "machineType": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/n1-standard-2",
        ...
      }
      

      Replace the following:

      • PROJECT_ID: the project ID for this request.
      • ZONE: the zone for this instance.
      • INSTANCE_NAME: the name for the instance you want to export.
    4. Use the instances.update method in the Compute Engine API to update the target instance. If you are updating a property that requires an instance restart, include the mostDisruptiveAllowedAction=RESTART query parameter to indicate that the instance can be restarted during the update. If the properties don't require a restart, specify the mostDisruptiveAllowedAction=REFRESH query parameter. To learn which properties require a restart, see the updatable properties list in this document. Include the full body for the instance resource that you modified. In the following example, the body changes the machine type to n1-standard-2:

      PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME?mostDisruptiveAllowedAction=ALLOWED_ACTION
      
      {
        ...
        "machineType": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/machineTypes/n1-standard-2",
        ...
      }
      

      Replace the following:

      • PROJECT_ID: the project ID for this request.
      • ZONE: the zone for this instance.
      • INSTANCE_NAME: the name for the instance you want to export.
      • ALLOWED_ACTION: defines how disruptive the update can be. Specify RESTART to allow for a full restart of the instance. Specify REFRESH to update the instance only if the modified properties don't require the instance to restart.

    If the update request is valid and the required resources are available, the instance update process begins. You can monitor the status of this operation by viewing the audit logs. If the update requires a restart and your command allows for a RESTART, the instance restarts to apply the changes.

    What's next