Terraform を使用して Service Usage リソースを管理する

このドキュメントでは、Terraform を使用して、割り当てのオーバーライドなどの Service Usage リソースをプロビジョニングする方法について説明します。

コンシューマー割り当てオーバーライドを管理するには、google_service_usage_consumer_quota_override スキーマを使用して Terraform 構成ファイルにリソースを指定し、Terraform コマンドを実行して変更を適用します。

準備

このチュートリアルで使用する用語を説明するサービス割り当てモデルを確認します。

Terraform と認証情報を構成します。

  1. Terraform をインストールします

  2. Google Cloud SDK をインストールします

  3. アプリケーションのデフォルト認証情報を作成します。

    gcloud auth application-default login
    

コンシューマー割り当てオーバーライドを作成する

  1. 構成ファイルを作成します。

    1. 作業ディレクトリに移動します。

    2. 次の内容をコピーして、main.tf という新しいファイルに貼り付けます。

    resource "google_service_usage_consumer_quota_override" "regional_override" {
      provider       = google-beta
      dimensions = {
        region = "us-central1"
      }
      project        = PROJECT_ID
      service        = "libraryagent.googleapis.com"
      metric         = "libraryagent.googleapis.com%2Fread_requests_regional"
      limit          = "%2Fmin%2Fproject%2Fregion"
      override_value = "8"
      force          = true
    }
    
    resource "google_service_usage_consumer_quota_override" "cd_override" {
      provider       = google-beta
      dimensions = {
        author = "larry"
      }
      project        = PROJECT_ID
      service        = "libraryagent.googleapis.com"
      metric         = "libraryagent.googleapis.com%2Fborrows"
      limit          = "%2Fauthor%2Fproject"
      override_value = "8"
      force          = true
    }
    

    main.tf ファイルに貼り付けた内容には、2 つのコンシューマ オーバーライド("regional_override""cd_override")の構成情報が含まれています。各フィールドの説明については、引数リファレンスをご覧ください。

  2. 作業ディレクトリを初期化します。

    terraform init -upgrade
    
  3. Terraform 構成を適用します。

    terraform apply
    

    Terraform は現在の状態を調べ、構成ファイルの情報と比較します。比較が完了すると、Terraform によって変更のリストが出力され、変更の適用を続行するか、キャンセルするかのオプションが表示されます。

    GoSM: Setting up security policy for terraform binary.
    GoSM: Setting up security policy for terraform binary.
    google_service_usage_consumer_quota_override.cd_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # google_service_usage_consumer_quota_override.cd_override will be created
      + resource "google_service_usage_consumer_quota_override" "cd_override" {
          + dimensions     = {
              + "author" = "larry"
            }
          + force          = true
          + id             = (known after apply)
          + limit          = "%2Fauthor%2Fproject"
          + metric         = "libraryagent.googleapis.com%2Fborrows"
          + name           = (known after apply)
          + override_value = "8"
          + project        = "loas-linweic"
          + service        = "libraryagent.googleapis.com"
        }
    
      # google_service_usage_consumer_quota_override.regional_override will be created
      + resource "google_service_usage_consumer_quota_override" "regional_override" {
          + dimensions     = {
              + "region" = "us-central1"
            }
          + force          = true
          + id             = (known after apply)
          + limit          = "%2Fmin%2Fproject%2Fregion"
          + metric         = "libraryagent.googleapis.com%2Fread_requests_regional"
          + name           = (known after apply)
          + override_value = "8"
          + project        = "loas-linweic"
          + service        = "libraryagent.googleapis.com"
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    
    

    「はい」と入力します。

    Enter a value: yes
    
    google_service_usage_consumer_quota_override.regional_override: Creating...
    google_service_usage_consumer_quota_override.cd_override: Creating...
    google_service_usage_consumer_quota_override.cd_override: Still creating... [10s elapsed]
    google_service_usage_consumer_quota_override.regional_override: Still creating... [10s elapsed]
    google_service_usage_consumer_quota_override.cd_override: Creation complete after 12s [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    google_service_usage_consumer_quota_override.regional_override: Creation complete after 12s [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    

コンシューマー割り当てオーバーライドを変更する

  1. オーバーライドを変更するには、main.tf ファイルを編集し、override_value フィールドの値を変更します。
  2. Terraform 構成を適用します。

    terraform apply
    

コンシューマー割り当てオーバーライドを削除する

  1. コンシューマー割り当てオーバーライドを削除するには、main.tf ファイルから対応するリソース ブロックを削除します。複数のオーバーライドを削除できます。すべてのオーバーライドを削除するには、main.tf ファイルが空であることを確認します。

  2. Terraform 構成を適用します。

    terraform apply
    

    たとえば、以前にプロビジョニングしたリージョン オーバーライドを削除するには、次のようにします。リソースを参照するブロックを削除します。

    resource "google_service_usage_consumer_quota_override" "regional_override" {
      ...
    }
    

    terraform apply の後の出力は次のとおりです。

    GoSM: Setting up security policy for terraform binary.
    GoSM: Setting up security policy for terraform binary.
    google_service_usage_consumer_quota_override.regional_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    google_service_usage_consumer_quota_override.cd_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # google_service_usage_consumer_quota_override.regional_override will be destroyed
      - resource "google_service_usage_consumer_quota_override" "regional_override" {
          - dimensions     = {
              - "region" = "us-central1"
            } -> null
          - force          = true -> null
          - id             = "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=" -> null
          - limit          = "%2Fmin%2Fproject%2Fregion" -> null
          - metric         = "libraryagent.googleapis.com%2Fread_requests_regional" -> null
          - name           = "Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=" -> null
          - override_value = "8" -> null
          - project        = "loas-linweic" -> null
          - service        = "libraryagent.googleapis.com" -> null
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    

    「はい」と入力して続行します。

    Enter a value: yes
    
    google_service_usage_consumer_quota_override.regional_override: Destroying... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    google_service_usage_consumer_quota_override.regional_override: Still destroying... [id=projects/loas-linweic/services/librarya...JyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=, 10s elapsed]
    google_service_usage_consumer_quota_override.regional_override: Destruction complete after 11s
    
    Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
    

状態ファイル

作業ディレクトリに、terraform.tfstate と terraform.tfstate.backup の 2 つのファイルが生成されていることに注目してください。最初のファイルには、最新の terraform apply の実行後にインフラストラクチャの現在の状態が記録されます。2 番目のファイルには、最新の terraform apply より前の状態が記録されます。Terraform の状態について詳しくは、公式ドキュメントをご覧ください。

ここまでこのページの手順を実施してきた場合は、terraform.tfstate ファイルに次の内容が含まれ、cd_override がインフラストラクチャに残っています。

  {
    "version": 4,
    "terraform_version": "0.12.31",
    "serial": 49,
    "lineage": "9dfbb2cc-7014-10ec-b8e1-ec9f36ea1acc",
    "outputs": {},
    "resources": [
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "cd_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "author": "larry"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "limit": "%2Fauthor%2Fproject",
              "metric": "libraryagent.googleapis.com%2Fborrows",
              "name": "Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      }
    ]
  }

terraform.tfstate.backup ファイルには、デプロビジョニング前の両方のオーバーライドが含まれます。

  {
    "version": 4,
    "terraform_version": "0.12.31",
    "serial": 47,
    "lineage": "9dfbb2cc-7014-10ec-b8e1-ec9f36ea1acc",
    "outputs": {},
    "resources": [
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "cd_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "author": "larry"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "limit": "%2Fauthor%2Fproject",
              "metric": "libraryagent.googleapis.com%2Fborrows",
              "name": "Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      },
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "regional_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "region": "us-central1"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=",
              "limit": "%2Fmin%2Fproject%2Fregion",
              "metric": "libraryagent.googleapis.com%2Fread_requests_regional",
              "name": "Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      }
    ]
  }