アイドル状態の予約に関する推奨事項を表示して適用する


このページでは、アイドル状態の予約に関する推奨事項を使用してアイドル状態のオンデマンド予約を特定して削除し、使用していないリソースに対して課金されないようにする方法について説明します。

始める前に

  • 制限事項を参照して、予約がアイドル状態の予約に関する推奨事項をサポートしているかどうかを確認します。
  • まだ設定していない場合は、認証を設定します。認証とは、Google Cloud サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のように Compute Engine に対する認証を行います。

    このページのサンプルをどのように使うかに応じて、タブを選択してください。

    コンソール

    Google Cloud コンソールを使用して Google Cloud サービスと API にアクセスする場合、認証を設定する必要はありません。

    gcloud

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

      gcloud init
    2. デフォルトのリージョンとゾーンを設定します

    REST

    このページの REST API サンプルをローカル開発環境で使用するには、gcloud CLI に指定した認証情報を使用します。

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

      gcloud init

アイドル状態の予約に関する推奨事項を表示する

アイドル状態の予約に関する推奨事項を表示するには、次のいずれかの方法を使用します。

コンソール

Google Cloud コンソールで、FinOps ハブに移動します。

FinOps ハブに移動

gcloud

アイドル状態の予約に関する推奨事項を表示するには、--recommender=google.compute.IdleResourceRecommender フラグを指定して gcloud recommender recommendations list コマンドを使用します。

gcloud recommender recommendations list \
  --location=ZONE \
  --recommender=google.compute.IdleResourceRecommender \
  --format=yaml

ZONE は、推奨事項を一覧表示する予約を含むゾーンに置き換えます。

次に例を示します。

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

ロケーションにアイドル状態の VM がない場合、レスポンスは空になります。それ以外の場合、レスポンスには推奨事項ごとに次のフィールドが含まれます。


---
associatedInsights:
- insight: projects/953727763714/locations/us-central1-f/insightTypes/google.compute.IdleResourceInsight/insights/ecf77776-4a2a-4e6e-aad2-b7f9c632e5f9
content:
operationGroups:
- operations:
  - action: remove
    path: /
    resource: //compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation
    resourceType: compute.googleapis.com/Reservation
overview:
  currentReservation:
    machineType: n1-standard-1
    reservationName: test-reservation
    reservedMachineCount: '1'
    zone: us-central1-f
description: Save cost by deleting the idle reservation test-reservation
etag: '"aff724ab8e40e1ef"'
lastRefreshTime: '2024-06-10T07:00:00Z'
name: projects/953727763714/locations/us-central1-f/recommenders/google.compute.IdleResourceRecommender/recommendations/56f8ac5d-8380-4cb8-a76f-bbc270fb4b8e
primaryImpact:
category: COST
costProjection:
  cost:
    currencyCode: USD
    nanos: -199820000
    units: '-1072'
  costInLocalCurrency:
    currencyCode: USD
    nanos: -199820000
    units: '-1072'
  duration: 2592000s
priority: P2
recommenderSubtype: DELETE_RESERVATION
stateInfo:
state: ACTIVE
targetResources:
- //compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation

Google Cloud CLI を使用した推奨事項の活用について詳しくは、gcloud の例をご覧ください。

REST

アイドル状態の予約に関する推奨事項を表示するには、recommendations.list メソッドGET リクエストを行い、次の推奨事項タイプを使用します。

  • google.compute.IdleResourceRecommender

API 呼び出しは次のようになります。

GET https://recommender.googleapis.com/v1/projects/PROJECT_ID/locations/ZONE/recommenders/google.compute.IdleResourceRecommender/recommendations

次のように置き換えます。

  • PROJECT_ID: オブジェクトの ID。
  • ZONE: 推奨を一覧表示するインスタンスを含むゾーン。

次の例は、curl を使用してリクエストを送信する方法と、関連するサンプル レスポンスを示しています。

nimishc - this response needs to be updated

PROJECT_ID=my-project
ZONE=us-central1-c
RECOMMENDER_ID=google.compute.IdleResourceRecommender

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "x-goog-user-project: $PROJECT_ID" \
  https://recommender.googleapis.com/v1/projects/$PROJECT_ID/locations/$ZONE/recommenders/$RECOMMENDER_ID/recommendations

アイドル状態の予約に関する推奨事項に対する JSON レスポンスの例:

{
  "associatedInsights": [
    {
      "insight": "projects/9531234763714/locations/us-central1-f/insightTypes/google.compute.IdleResourceInsight/insights/ecf77776-4a2a-4e6e-aad2-b7f9c632e5f9"
    }
  ],
  "content": {
    "operationGroups": [
      {
        "operations": [
          {
            "action": "remove",
            "path": "/",
            "resource": "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation",
            "resourceType": "compute.googleapis.com/Reservation"
          }
        ]
      }
    ],
    "overview": {
      "currentReservation": {
        "machineType": "n1-standard-1",
        "reservationName": "test-reservation",
        "reservedMachineCount": "1",
        "zone": "us-central1-f"
      }
    }
  },
  "description": "Save cost by deleting the idle reservation test-reservation",
  "etag": "\"8d5603cac8fa9342\"",
  "lastRefreshTime": "2024-06-11T07:00:00Z",
  "name": "projects/9531234763714/locations/us-central1-f/recommenders/google.compute.IdleResourceRecommender/recommendations/56f8ac5d-8380-4cb8-a76f-bbc270fb4b8e",
  "primaryImpact": {
    "category": "COST",
    "costProjection": {
      "cost": {
        "currencyCode": "USD",
        "nanos": -199820000,
        "units": "-1072"
      },
      "costInLocalCurrency": {
        "currencyCode": "USD",
        "nanos": -199820000,
        "units": "-1072"
      },
      "duration": "2592000s"
    }
  },
  "priority": "P2",
  "recommenderSubtype": "DELETE_RESERVATION",
  "stateInfo": {
    "state": "ACTIVE"
  },
  "targetResources": [
    "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation"
  ]
}

各フィールドの詳細については、Recommender API のドキュメントをご覧ください。

推奨のレスポンスの解釈

gcloud CLI または REST を介して受信する各推奨には、推奨を適用するために連続して実行できるオペレーション グループが含まれています。アイドル状態の予約の場合、オペレーション グループには予約を削除する remove オペレーションが含まれます。

{
  "action": "remove",
  "path": "/",
  "resource": "//compute.googleapis.com/projects/committed-use-discount-test/zones/us-central1-f/reservations/test-reservation",
  "resourceType": "compute.googleapis.com/Reservation"
}

アイドル状態の予約に関する推奨事項を適用する

アイドル状態の予約に関する推奨事項を受け取り、リソースが不要になったと判断した場合は、予約を削除します。

次のステップ