Check if a given instance is Spot VM or not
Stay organized with collections
Save and categorize content based on your preferences.
This sample checks if a given instance is Spot VM or not.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, and Python to check if a given Compute Engine instance is a Spot VM.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples use the Compute Engine API to retrieve instance details and check the provisioning model, which determines if the instance is a Spot VM.\u003c/p\u003e\n"],["\u003cp\u003eBefore running the code samples, users must follow setup instructions for their chosen language and set up Application Default Credentials (ADC) for authentication.\u003c/p\u003e\n"],["\u003cp\u003eEach language provides a function (e.g., \u003ccode\u003eisSpotVM\u003c/code\u003e in Go, \u003ccode\u003eisSpotVm\u003c/code\u003e in Java and Python) that accepts the project ID, zone, and instance name as parameters.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples output whether or not the instance is a Spot VM based on the instance's provisioning model from the API.\u003c/p\u003e\n"]]],[],null,["# Check if a given instance is Spot VM or not\n\nThis sample checks if a given instance is Spot VM or not.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and use Spot VMs](/compute/docs/instances/create-use-spot)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \t\"cloud.google.com/go/compute/apiv1/computepb\"\n )\n\n // isSpotVM checks if a given instance is a Spot VM or not.\n func isSpotVM(w io.Writer, projectID, zone, instanceName string) (bool, error) {\n \t// projectID := \"your_project_id\"\n \t// zone := \"europe-central2-b\"\n \t// instanceName := \"your_instance_name\"\n \tctx := context.Background()\n \tclient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_NewInstancesRESTClient(ctx)\n \tif err != nil {\n \t\treturn false, fmt.Errorf(\"NewInstancesRESTClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \treq := &computepb.GetInstanceRequest{\n \t\tProject: projectID,\n \t\tZone: zone,\n \t\tInstance: instanceName,\n \t}\n\n \tinstance, err := client.Get(ctx, req)\n \tif err != nil {\n \t\treturn false, fmt.Errorf(\"GetInstance: %w\", err)\n \t}\n\n \tisSpot := instance.GetScheduling().GetProvisioningModel() == computepb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1/computepb.html#cloud_google_com_go_compute_apiv1_computepb_Scheduling_UNDEFINED_PROVISIONING_MODEL_Scheduling_RESERVATION_BOUND_Scheduling_SPOT_Scheduling_STANDARD.String()\n\n \tvar isSpotMessage string\n \tif !isSpot {\n \t\tisSpotMessage = \" not\"\n \t}\n \tfmt.Fprintf(w, \"Instance %s is%s spot\\n\", instanceName, isSpotMessage)\n\n \treturn instance.GetScheduling().GetProvisioningModel() == computepb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1/computepb.html#cloud_google_com_go_compute_apiv1_computepb_Scheduling_UNDEFINED_PROVISIONING_MODEL_Scheduling_RESERVATION_BOUND_Scheduling_SPOT_Scheduling_STANDARD.String(), nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Scheduling.html;\n import java.io.IOException;\n import java.util.concurrent.ExecutionException;\n import java.util.concurrent.TimeoutException;\n\n public class CheckIsSpotVm {\n public static void main(String[] args)\n throws IOException, ExecutionException, InterruptedException, TimeoutException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Google Cloud project you want to use.\n String projectId = \"your-project-id\";\n // Name of the virtual machine to check.\n String instanceName = \"your-route-name\";\n // Name of the zone you want to use. For example: \"us-west3-b\"\n String zone = \"your-zone\";\n\n boolean isSpotVm = isSpotVm(projectId, instanceName, zone);\n System.out.printf(\"Is %s spot VM instance - %s\", instanceName, isSpotVm);\n }\n\n // Check if a given instance is Spot VM or not.\n public static boolean isSpotVm(String projectId, String instanceName, String zone)\n throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html instance = client.get(projectId, zone, instanceName);\n\n return instance.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html#com_google_cloud_compute_v1_Instance_getScheduling__().getProvisioningModel()\n .equals(https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Scheduling.html.ProvisioningModel.SPOT.name());\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/compute/latest/\n\n\n def is_spot_vm(project_id: str, zone: str, instance_name: str) -\u003e bool:\n \"\"\"\n Check if a given instance is Spot VM or not.\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n zone: name of the zone you want to use. For example: \"us-west3-b\"\n instance_name: name of the virtual machine to check.\n Returns:\n The Spot VM status of the instance.\n \"\"\"\n instance_client = https://cloud.google.com/python/docs/reference/compute/latest/.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient.html()\n instance = instance_client.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient.html#google_cloud_compute_v1_services_instances_InstancesClient_get(\n project=project_id, zone=zone, instance=instance_name\n )\n return (\n instance.scheduling.provisioning_model\n == https://cloud.google.com/python/docs/reference/compute/latest/.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.Scheduling.html.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.Scheduling.ProvisioningModel.html.SPOT.name\n )\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]