Manage Service Usage resources with Terraform

This document describes how to provision Service Usage resources such as quota overrides using Terraform.

To manage consumer quota overrides, you can use the google_service_usage_consumer_quota_override schema to specify resources in Terraform configuration file and run Terraform commands to apply the change.

Before you begin

Review the service quota model which describes the terminology used in this tutorial.

Configure Terraform and credentials:

  1. Install Terraform.

  2. Install Google Cloud SDK.

  3. Create your Application Default Credentials:

    gcloud auth application-default login
    

Create Consumer Quota Overrides

  1. Create configuration file:

    1. Navigate to your working directory.

    2. Copy and paste the following content into a new file named 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
    }
    

    The content you placed in your main.tf file contains the configuration information for two consumer overrides: "regional_override" and "cd_override". Refer to the Argument Reference on explanations of each field.

  2. Initialize the working directory.

    terraform init -upgrade
    
  3. Apply the Terraform configuration.

    terraform apply
    

    Terraform inspects the current state and compares it to the information in the configuration file. After the comparison is complete, Terraform prints a list of changes, and then provides options to proceed or cancel applying the changes.

    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 yes:

    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.
    

Modify Consumer Quota Overrides

  1. To modify an override, edit your main.tf file and change value of an override_value field.
  2. Apply the Terraform configuration.

    terraform apply
    

Delete Consumer Quota Overrides

  1. To delete a consumer quota override, remove the corresponding resource block from the main.tf file. You can remove multiple overrides. To remove all overrides, ensure that you main.tf file is empty.

  2. Apply the Terraform configuration.

    terraform apply
    

    For example, to delete the regional override you provisioned previously. Delete the block referring to the resource:

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

    The output after terraform apply is the following:

    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 yes to proceed:

    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.
    

State Files

You may notice that in your working directory there are two generated files: terraform.tfstate and terraform.tfstate.backup. The first file records the current state of your infrastructure after you execute the latest terraform apply. The second file records the state prior to your latest terraform apply. For more information of the Terraform State, refer to their official doc.

If you have followed the steps in this page so far, the following content is in the terraform.tfstate file, with the cd_override left in our infrastructure.

  {
    "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"
          }
        ]
      }
    ]
  }

In the terraform.tfstate.backup file, it contains both overrides prior to the deprovision.

  {
    "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"
          }
        ]
      }
    ]
  }