Limit the run time of a VM


This document explains how to limit the run time of new or existing virtual machine (VM) instances and how to monitor the run time of those VMs. For managed instance groups (MIGs), see Limit the run time of a MIG instead.

When limiting the run time of a VM, you can schedule the VM to be automatically terminated (stopped or deleted) when it reaches a specific time limit (duration or time). Use time limits to help optimize temporary workloads—by automatically limiting VM run times, you can help minimize costs and free up quotas.

To learn how to immediately stop a VM, see Stop or restart a VM. To learn how to immediately delete a VM, see Delete a VM.

Before you begin

  • To get the permissions that you need to limit the run time of a VM, ask your administrator to grant you the Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM role on the project. For more information about granting roles, see Manage access to projects, folders, and organizations.

    You might also be able to get the required permissions through custom roles or other predefined roles.

  • 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

    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.

    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, then initialize it by running the following command:

      gcloud init

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Restrictions

Limiting the run time of a VM has the following restrictions:

  • The automatic termination action must be either stop or delete. You cannot configure a VM to be automatically suspended when the time limit is reached.

  • The minimum time limit is 30 seconds and the maximum time limit is 120 days.

  • Automatic termination might take up to 30 seconds longer than the duration or time you specify to begin stopping or deleting the VM.

  • You cannot use time limits with legacy preemptible VMs. Instead, use time limits with Spot VMs.

  • A VM's local SSD data cannot be preserved when the VM is automatically stopped due to a time limit. For more information, see Stop a VM with Local SSD.

Limit the run time of a new VM

The following sections describe how to configure a time limit while creating a new VM. You can specify the time limit, which is when you want a VM to automatically terminate, as either a duration (maxRunDuration) or a time (terminationTime).

When deciding the type of time limit for a VM, consider that the VM might be interrupted—for example, by a user request or by a host event. The behavior of a time limit varies based on the type of interruption and the type of time limit:

  • A VM's termination timestamp (terminationTimestamp) is a read-only field that represents the planned time for automatic termination and is automatically defined by Compute Engine whenever a VM has a time limit and enters the RUNNING state.

  • The termination timestamp is automatically cleared whenever a VM is stopped or suspended. However, a termination timestamp doesn't change when you reset a VM or reboot a VM.

  • The termination timestamp is automatically redefined whenever the VM is restarted or resumed based on the type of time limit that you set:

    • If you set a duration for the VM, the termination timestamp is recalculated by adding that duration to the VM's latest start time.
    • If you set a time for the VM, the termination timestamp is set to that time. However, the time must be in the future; otherwise, any requests to create or rerun the VM fail until you update or remove the time.

Set a duration

A duration represents the total run time that you want for a VM. To create a VM that is automatically terminated after the VM runs for a specific duration, use the Google Cloud console, Google Cloud CLI, Terraform, or Compute Engine API.

Console

  1. In the Google Cloud console, go to the Create an instance page.

    Go to Create an instance

  2. In the Availability policies section, expand VM provisioning model advanced settings.

  3. Select the Set a time limit for the VM checkbox. The Time limit type field appears.

  4. In the Time limit type field, select By hours (default) to specify the time limit as a duration. In the following field, enter the duration in hours.

  5. In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:

    • To automatically stop the VM, select Stop (default).
    • To delete the VM, select Delete.
  6. Optional: Specify other VM options. For more information, see Creating and starting a VM instance.

  7. To create and start the VM, click Create.

gcloud

To create a VM from the gcloud CLI, use the gcloud compute instances create command.

  • To create a VM that is automatically terminated after a specific duration, you must include the --max-run-duration flag.
  • To specify the termination action, include the --instance-termination-action flag.
  • Configure the --discard-local-ssds-at-termination-timestamp flag:
    • If the VM has any local SSDs and the termination action (TERMINATION_ACTION) is stop (STOP), you must include the --discard-local-ssds-at-termination-timestamp=true flag.
    • Otherwise, omit the --discard-local-ssds-at-termination-timestamp flag.
gcloud compute instances create VM_NAME \
    --max-run-duration=DURATION \
    --instance-termination-action=TERMINATION_ACTION

Replace the following:

  • VM_NAME: The name of the new VM.
  • DURATION: The duration you want this VM to run before being automatically terminated. Format the duration as the number of days, hours, minutes, and seconds followed by d, h, m, and s respectively. For example, specify 30m for a duration of 30 minutes, or specify 1d2h3m4s for a duration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimum duration is 30 seconds (30s) and the maximum duration is 120 days (120d).
  • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model:
    • If this is a Spot VM (if the VM uses the --provisioning-model=SPOT flag), the --instance-termination-action=TERMINATION_ACTION flag is optional. If this flag is omitted, the default termination action is stop.
    • Otherwise (default), the --instance-termination-action=TERMINATION_ACTION flag is required.

For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.

Terraform

To create a VM using Terraform, use the google_compute_instance resource.

  • To create a VM that is automatically terminated after a specific duration, you must include the max_run_duration argument.
  • To specify the termination action, include the instance_termination_action argument. You must include the instance_termination_action argument unless you are creating a Spot VM (set the provisioning_model argument to SPOT), which defaults to stop (STOP).
  • Configure the on_instance_stop_action argument:
    • If the VM has any local SSDs and the termination action (instance_termination_action argument) is stop (STOP), you must set the on_instance_stop_action argument to true (true).
    • Otherwise, omit the on_instance_stop_action argument.

For more information, see the Terraform documentation for the google_compute_instance resource.

REST

To create a VM from the Compute Engine API, use the instances.insert method. You must specify a name, machine type, and boot disk for the VM.

To create a VM that is automatically terminated after a specific duration, you must include the maxRunDuration field. To specify the termination action, include the instanceTerminationAction field, which is optional for Spot VMs.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "name": "VM_NAME",
  "disks": [
    {
      "initializeParams": {
        "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
      },
      "boot": true
    }
  ]
  "scheduling":
  {
    "maxRunDuration":
    {
      "seconds": DURATION
    },
    "instanceTerminationAction": "TERMINATION_ACTION"
  },
}

Replace the following:

  • PROJECT_ID: The project id of the project to create the VM in.
  • ZONE: The zone to create the VM in. The zone must also support the machine type to use for the new VM.
  • MACHINE_TYPE: The predefined or custom machine type for the new VM.
  • VM_NAME: The name of the new VM.
  • IMAGE_PROJECT: The project containing the image. For example, if you specify family/debian-10 as the image, specify debian-cloud as the image project.
  • IMAGE: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10 as the image and debian-cloud as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.
  • DURATION: The duration in seconds that you want this VM to run before being automatically terminated. The minimum duration is 30 seconds (30s) and the maximum duration is 120 days (120d).
  • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model:
    • If this is a Spot VM (if the VM uses the "provisioningModel": "SPOT" field), the "instanceTerminationAction": "TERMINATION_ACTION" field is optional. If this field is omitted, the default termination action is stop.
    • Otherwise (default), the "instanceTerminationAction": "TERMINATION_ACTION" field is required.

For more information about the options you can specify when creating a VM, see Creating and starting a VM instance.

Set a time

A time represents the date, time, and timezone when you want a VM to be terminated. To create a VM that is automatically terminated at a specific time, use the Google Cloud console, Google Cloud CLI, or Compute Engine API.

Console

  1. In the Google Cloud console, go to the Create an instance page.

    Go to Create an instance

  2. In the Availability policies section, expand VM provisioning model advanced settings.

  3. Select the Set a time limit for the VM checkbox. The Time limit type field appears.

  4. In the Time limit type field, select By date to specify the time limit as a time and date. In the following field, click Select date and time and select the date, time, and timezone for the time limit.

  5. In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:

    • To automatically stop the VM, select Stop (default).
    • To delete the VM, select Delete.
  6. Optional: Specify other VM options. For more information, see Creating and starting a VM instance.

  7. To create and start the VM, click Create.

gcloud

To create a VM from the gcloud CLI, use the gcloud compute instances create command.

  • To create a VM that is automatically terminated at a specific time, you must include the --termination-time flag.
  • To specify the termination action, include the --instance-termination-action flag, which is optional for Spot VMs.
  • Configure the --discard-local-ssds-at-termination-timestamp flag:
    • If the VM has any local SSDs and sets the termination action (TERMINATION_ACTION) to stop (STOP), you must include the --discard-local-ssds-at-termination-timestamp=true flag.
    • Otherwise, omit the --discard-local-ssds-at-termination-timestamp flag.
gcloud compute instances create VM_NAME \
    --termination-time=TIME \
    --instance-termination-action=TERMINATION_ACTION

Replace the following:

  • VM_NAME: The name of the new VM.
  • TIME: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:

    YYYY-MM-DDTHH:MM:SSOFFSET
    

    Replace the following:

    • YYYY-MM-DD: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.
    • HH:MM:SS: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.
    • OFFSET: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00. Alternatively, to use no offset (UTC+0), specify Z.
  • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model:

    • If this is a Spot VM (if the VM uses the --provisioning-model=SPOT flag), the --instance-termination-action=TERMINATION_ACTION flag is optional. If this flag is omitted, the default termination action is stop.
    • Otherwise (default), the --instance-termination-action=TERMINATION_ACTION flag is required.

For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.

REST

To create a VM from the Compute Engine API, use the instances.insert method. You must specify a name, machine type, and boot disk for the VM.

To create a VM that is automatically terminated at a specific time, you must include the terminationTime field. To specify the termination action, include the instanceTerminationAction field, which is optional for Spot VMs.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "name": "VM_NAME",
  "disks": [
    {
      "initializeParams": {
        "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
      },
      "boot": true
    }
  ]
  "scheduling":
  {
    "terminationTime": "TIME",
    "instanceTerminationAction": "TERMINATION_ACTION"
  },
}

Replace the following:

  • PROJECT_ID: The project id of the project to create the VM in.
  • ZONE: The zone to create the VM in. The zone must also support the machine type to use for the new VM.
  • MACHINE_TYPE: The predefined or custom machine type for the new VM.
  • VM_NAME: The name of the new VM.
  • IMAGE_PROJECT: The project containing the image. For example, if you specify family/debian-10 as the image, specify debian-cloud as the image project.
  • IMAGE: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10 as the image and debian-cloud as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.
  • TIME: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:

    YYYY-MM-DDTHH:MM:SSOFFSET
    

    Replace the following:

    • YYYY-MM-DD: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.
    • HH:MM:SS: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.
    • OFFSET: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00. Alternatively, to use no offset (UTC+0), specify Z.
  • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model::

    • If this is a Spot VM (if the VM uses the "provisioningModel": "SPOT" field), the "instanceTerminationAction": "TERMINATION_ACTION" field is optional. If this field is omitted, the default termination action is stop.
    • Otherwise (default), the "instanceTerminationAction": "TERMINATION_ACTION" field is required.

For more information about the options you can specify when creating a VM, see Creating and starting a VM instance.

Limit the run time of an existing VM

You can limit the run time of an existing VM by updating the VM's schedule. If you don't already understand how to configure settings for time limits, review the previous sections about how to limit the run time of a new VM first.

You can use the Google Cloud console, Google Cloud CLI, or Compute Engine API to update the VM's scheduling-related properties, as described in this section. This method requires that you first stop your VM, then update its properties, then restart it. Alternatively, if you want to simultaneously update additional VM properties and automatically stop and restart a VM, see Update instance properties instead.

Console

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. In the Name column, click the name of the VM that you want to update.

  3. From the VM instance details page, complete the following steps:

    1. If the VM is running, click Stop to stop the VM.
    2. To edit the VM, click Edit.
    3. On the Edit instance page, complete the following steps:

      1. In the Availability policies section, modify the Set a time limit for the VM checkbox and any fields below it as desired.

        For more information about how to configure the properties for time limits, see limit the run time of a new VM.

      2. To save your changes, click Save.

    4. Optional: If you want to start running the VM now, click Start.

gcloud

To update a VM's time limit by using the gcloud CLI, complete the following steps:

  1. If the VM is running, stop the VM by using the gcloud compute instances stop command:

    gcloud compute instances stop VM_NAME
    

    Replace VM_NAME with the name of VM that you want to update.

  2. Update the VM's time limit by using the gcloud compute instances set-scheduling command.

    gcloud compute instances set-scheduling VM_NAME \
        --max-run-duration=DURATION \
        --termination-time=TIME \
        --instance-termination-action=TERMINATION_ACTION \
        --discard-local-ssds-at-termination-timestamp=true
    

    Configure the following flags:

    • You must omit either the --max-run-duration flag or the --termination-time flag to set the time limit as either a time or duration, respectively.
    • Include --instance-termination-action flag to set the termination action.
    • If the VM has any local SSDs and sets the termination action (TERMINATION_ACTION) to stop (STOP), you must include the --discard-local-ssds-at-termination-timestamp=true flag. Otherwise, omit the --discard-local-ssds-at-termination-timestamp=true flag.

    Then, replace the following:

    • VM_NAME: The name of the VM that you want to update.
    • DURATION: The duration you want this VM to run before being automatically terminated. Format the duration as the number of days, hours, minutes, and seconds followed by d, h, m, and s respectively. For example, specify 30m for a duration of 30 minutes, or specify 1d2h3m4s for a duration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimum duration is 30 seconds (30s) and the maximum duration is 120 days (120d).
    • TIME: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:

      YYYY-MM-DDTHH:MM:SSOFFSET
      

      Replace the following:

      • YYYY-MM-DD: a date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.
      • HH:MM:SS: a time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.
      • OFFSET: the time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00. Alternatively, to use no offset (UTC+0), specify Z.
    • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model:

      • If this is a Spot VM (if the VM uses the --provisioning-model=SPOT flag), the --instance-termination-action=TERMINATION_ACTION flag is optional. If this flag is omitted, the default termination action is stop.
      • Otherwise (default), the --instance-termination-action=TERMINATION_ACTION flag is required.
  3. If you want the VM to start running, start the VM by using the gcloud compute instances start command:

    gcloud compute instances start VM_NAME
    

    Replace VM_NAME with the name of the VM.

REST

To update a VM's time limit by using the Compute Engine API, complete the following steps:

  1. If the VM is running, stop the VM by using the instances.stop method:

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
    

    Replace the following:

    • PROJECT_ID: The project id of the project that contains the VM.
    • ZONE: The zone that contains the VM.
    • VM_NAME: The name of the VM that you want to update.
  2. Update the VM's scheduling property, including fields for the VM's time limit, by using the instances.setScheduling method. Include either the maxRunDuration field or the terminationTime field to set the time limit as either a duration or time, respectively:

    • To set a duration, use the following request:

      POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setScheduling
      {
        "maxRunDuration":
        {
          "seconds": DURATION
        },
        "instanceTerminationAction": "TERMINATION_ACTION"
      }
      
    • To set a time, use the following request:

      POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setScheduling
      {
        "terminationTime": "TIME",
        "instanceTerminationAction": "TERMINATION_ACTION"
      }
      

    Replace the following:

    • PROJECT_ID: The project id of the project to create the VM in.
    • ZONE: The zone to create the VM in. The zone must also support the machine type to use for the new VM.
    • MACHINE_TYPE: The predefined or custom machine type for the new VM.
    • VM_NAME: The name of the new VM.
    • IMAGE_PROJECT: The project containing the image. For example, if you specify family/debian-10 as the image, specify debian-cloud as the image project.
    • IMAGE: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10 as the image and debian-cloud as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.
    • DURATION: The duration in seconds that you want this VM to run before being automatically terminated. The minimum duration is 30 seconds (30s) and the maximum duration is 120 days (120d).
    • TIME: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:

      YYYY-MM-DDTHH:MM:SSOFFSET
      

      Replace the following:

      • YYYY-MM-DD: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.
      • HH:MM:SS: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.
      • OFFSET: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00. Alternatively, to use no offset (UTC+0), specify Z.
    • TERMINATION_ACTION: The termination action for this VM, which can be either stop (STOP) or delete (DELETE). Whether this field is required or has a default value varies based on the VM's provisioning model:

      • If this is a Spot VM (if the VM uses the "provisioningModel": "SPOT" field), the "instanceTerminationAction": "TERMINATION_ACTION" field is optional. If this field is omitted, the default termination action is stop.
      • Otherwise (default), the "instanceTerminationAction": "TERMINATION_ACTION" field is required.
  3. If you want the VM to start running, start the VM by using the instances.start method:

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start
    

    Replace the following:

    • PROJECT_ID: the project id of the project that contains the VM.
    • ZONE: the zone that contains the VM.
    • VM_NAME: the name of the VM.

Monitor the run time of a VM

You can monitor the run time of a VM that has a time limit by using the following options:

  • To see when a running VM is scheduled to be automatically terminated, view the VM's termination timestamp:

    1. View the details of a VM.
    2. In the output, view the VM's field for the termination timestamp:
      • If you are using the Google Cloud console, see the Max duration field.
      • If you are using the Google Cloud CLI or Compute Engine API, see the terminationTimestamp field.
  • To verify if a VM was automatically terminated, view the VM's operations:

    1. View VM operations.
    2. In the output, you can identify VM operations that are caused by a time limit by looking for the following types of operations:

      • compute.instances.deferredStop indicates an automatic termination action of stop
      • compute.instances.deferredDelete indicates an automatic termination action of delete

What's next