Viewing and applying idle VM recommendations

Compute Engine provides idle recommendations to help you identify virtual machine (VM) instances that have not been used. These recommendations are generated automatically based on system metrics gathered by the Cloud Monitoring service over the previous 14 days. You can use idle VM recommendations to find and stop idle VM instances to reduce waste of resources and reduce your compute bill.

Before you begin

Pricing

Idle VM recommendations are available free of charge.

Limitations

For standalone VMs, idle recommendations are not available in following cases:

  • Instances with local SSDs
  • Instances with GPU/TPUs
  • App Engine flex resources
  • Dataflow resources
  • Google Kubernetes Engine resources

How detection of idle VM instances works

The Recommender generates recommendations about idle VM instances based on usage metrics from the last 14 days, or, for new VMs, starting from 5 days since VM creation. If CPU and network usage have been low during the observed period of time, the Recommender classifies the VM as idle.

To classify a VM as idle, all of the following conditions must be met:

  • CPU utilization is less than 0.03 vCPUs for 97% of VM runtime.
  • Received network traffic is less than 2000 bytes per second (B/s) for 95% of VM runtime.
  • Sent network traffic is less than 1000 B/s for 95% of VM runtime.

Frequency of recommendations

Idle VM recommendations are initially generated after a VM is created and running for 5 days. Subsequently, recommendations are updated at least once per day.

Relation with machine type sizing recommendations

Machine type sizing recommendations recommend the optimal size for a VM. You might receive a machine type recommendation to downsize an idle VM.

If you must keep an idle VM running despite its low utilization, use machine type recommendations to help you choose the optimal size for underutilized VMs.

Viewing idle VM instance recommendations

To view recommendations about idle VMs, use the gcloud tool or the API.

gcloud

Use the gcloud beta recommender recommendations list command with --recommender=google.compute.instance.IdleResourceRecommender:

gcloud beta recommender recommendations list \
  --project=PROJECT-ID \
  --location=ZONE \
  --recommender=google.compute.instance.IdleResourceRecommender \
  --format=yaml

Replace the following:

  • PROJECT-ID. The ID of your project.
  • ZONE. The zone for which to list recommendations, for example us-west1-a.

For example:

gcloud beta recommender recommendations list \
    --recommender=google.compute.instance.IdleResourceRecommender \
    --project=my-project \
    --location us-central1-c \
    --format=yaml

The response includes the following fields for each recommendation:


---
content:
  ...
  operationGroups:
  - operations:
    - action: test
      path: /status
      resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name
      resourceType: compute.googleapis.com/Instance
      value: RUNNING
    - action: replace
      path: /status
      resource: //compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name
      resourceType: compute.googleapis.com/Instance
      value: TERMINATED
description: Save cost by stopping Idle VM 'vm-name'.
etag: '"83da314c23f634e1"'
lastRefreshTime: '2020-02-24T07:56:40Z'
name: projects/141732092341/locations/us-central1-c/recommenders/google.compute.instance.IdleResourceRecommender/recommendations/0e061a3a-f921-4216-b1b4-62e16942cd1a
primaryImpact:
  category: COST
  costProjection:
    cost:
      currencyCode: USD
      nanos: -91533961
      units: '-262'
    duration: 2592000s
recommenderSubtype: STOP_VM
stateInfo:
  state: ACTIVE

Learn more about working with recommendations using gcloud in gcloud examples.

API

Call the beta recommendations.list method and use the following recommendation type:

  • google.compute.instance.IdleResourceRecommender

The API call looks like:

GET https://recommender.googleapis.com/beta/projects/PROJECT-ID/locations/ZONE/recommenders/google.compute.instance.IdleResourceRecommender/recommendations

Replace the following:

  • PROJECT-ID. The name of your project.
  • LOCATION. A zone that contains instances to list recommendations for, for example us-west1-a.

If you aren't already familiar with the authentication prerequisites for calling Google Cloud APIs, see the Authentication overview.

The following example bash script uses the end user authentication flow with an OAuth client credential. The script uses the Google oauth2l command-line tool to get an OAuth 2.0 access token and then makes a curl request using the token.

PROJECT_ID=my-project
ZONE=us-central1-c
RECOMMENDER=google.compute.instance.IdleResourceRecommender
OAUTH_JSON=~/service-account.json
OAUTH_HEADER="$(oauth2l header --json $OAUTH_JSON cloud-platform)"

curl -H "$OAUTH_HEADER" -H "x-goog-user-project: $PROJECT_ID" https://recommender.googleapis.com/beta/projects/$PROJECT_ID/locations/$ZONE/recommenders/google.compute.instance.IdleResourceRecommender/recommendations

Example JSON response for an idle VM recommendation:

{
 "description" : "Save cost by stopping Idle VM `vm-name`",
 "name": "projects/1574864402/locations/us-central1-c/recommenders/"
         "google.compute.instance.IdleResourceRecommender/"
         "recommendations/0fd31b24-cc05-4132-8431-ed54a22dd4f1",
 "lastRefreshTime": {
   "seconds": 1543912652
 },
 "primaryImpact": {
   "category": COST,
   "costProjection": {
    "cost": {"currencyCode": "USD", "units": -50},
    "duration": { "seconds": 2592000 }
   }
 },
 "stateInfo": ACTIVE,
 "content":
   "groups" : [
    {
      "operations" : [
       {
         "action": "test",
         "resourceType": "compute.googleapis.com/Instance",
         "resource": "//compute.googleapis.com/projects/my-project/"
                     "zones/us-central1-c/instances/vm-name",
         "path": "/status",
         "value": "RUNNING"
       },
       {
         "action": "replace",
         "resourceType": "compute.googleapis.com/Instance",
         "resource": "//compute.googleapis.com/projects/my-project/"
                     "zones/us-central1-c/instances/vm-name",
         "path": "/status",
         "value": "TERMINATED"
       }
      ]
    }
   ]
 },
 "etag" : "cb0e6ac2cfc0b591"
}

You can find more details about each field in the Recommender API documentation.

Interpreting the recommendation response

Each recommendation that you receive through the gcloud tool or API contains an operations group, with operations that you can perform in serial to apply the recommendation. Idle VM recommendation operation groups include two operations:

  1. A test operation to verify the current status of the VM. For example:

           {
             "action": "test",
             "resourceType": "compute.googleapis.com/Instance",
             "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name",
             "path": "/status",
             "value": "RUNNING"
           }
    
  2. A replace operation to change the status of the VM. For example:

           {
             "action": "replace",
             "resourceType": "compute.googleapis.com/Instance",
             "resource" : "//compute.googleapis.com/projects/my-project/zones/us-central1-c/instances/vm-name",
             "path": "/status",
             "value": "TERMINATED"
           }
    

The first operation is a test, which means that you should test that the resource is still RUNNING. You can do this by checking the VM state.

The second operation, replace, means that you should replace the resource's status with a new value, TERMINATED. You can do that by stopping the VM, which is described below.

Applying idle VM recommendations

After you receive an idle VM recommendation and you decide that you no longer need the instance, use the Google Cloud Console, the gcloud tool, or the API to stop and optionally delete the instance.

If you stop but do not delete an instance and its disks, you still pay for its disks.

Console

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

    Go to the VM instances page

  2. Select the instance that you want to stop.
  3. Click Stop.

gcloud

Use the instances stop command and specify the VM-NAME that you want to stop.

gcloud compute instances stop VM-NAME --zone ZONE

API

In the API, construct a POST request to stop an instance.

POST https://compute.googleapis.com/compute/v1/projects/MY-PROJECT/zones/ZONE/instances/VM-NAME/stop

If you're sure that VM and its disks can be removed you can delete the instance.

What's next