Administra recursos de uso del servicio con Terraform

En este documento, se describe cómo aprovisionar recursos de Service Usage, como anulaciones de cuota con Terraform.

Para administrar las anulaciones de cuotas de los consumidores, puedes usar el esquema google_service_usage_consumer_quota_override para especificar recursos en el archivo de configuración de Terraform y ejecutar comandos de Terraform para aplicar el cambio.

Antes de comenzar

Revisa el modelo de cuota de servicio, que describe la terminología que se usa en este instructivo.

Configura Terraform y las credenciales:

  1. Instala Terraform.

  2. Instala el SDK de Google Cloud.

  3. Crea tus credenciales predeterminadas de la aplicación:

    gcloud auth application-default login
    

Crea anulaciones de cuotas del consumidor

  1. Crea el archivo de configuración:

    1. Navega a tu directorio de trabajo.

    2. Copia y pega el siguiente contenido en un archivo nuevo llamado 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
    }
    

    El contenido que colocaste en el archivo main.tf incluye la información de configuración de dos anulaciones de consumidor: "regional_override" y "cd_override". Consulta la Referencia de argumentos para obtener explicaciones de cada campo.

  2. Inicializa el directorio de trabajo.

    terraform init -upgrade
    
  3. Aplicar la configuración de Terraform

    terraform apply
    

    Terraform inspecciona el estado actual y lo compara con la información del archivo de configuración. Una vez completada la comparación, Terraform imprime una lista de cambios y, luego, proporciona opciones para continuar o cancelar la aplicación de los cambios.

    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:
    
    

    Ingresa 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.
    

Modifica las anulaciones de cuotas del consumidor

  1. Para modificar una anulación, edita el archivo main.tf y cambia el valor de un campo override_value.
  2. Aplicar la configuración de Terraform

    terraform apply
    

Borra anulaciones de cuotas del consumidor

  1. Para borrar una anulación de cuota del consumidor, quita el bloque de recursos correspondiente del archivo main.tf. Puedes quitar varias anulaciones. Para quitar todas las anulaciones, asegúrate de que el archivo main.tf esté vacío.

  2. Aplica la configuración de Terraform.

    terraform apply
    

    Por ejemplo, para borrar la anulación regional que aprovisionaste con anterioridad. Borra el bloque que hace referencia al recurso:

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

    El resultado después de terraform apply es el siguiente:

    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:
    

    Ingresa yes para continuar:

    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.
    

Archivos de estado

Puedes observar que en tu directorio de trabajo hay dos archivos generados: terraform.tfstate y terraform.tfstate.backup. El primer archivo registra el estado actual de tu infraestructura después de que ejecutas el terraform apply más reciente. El segundo archivo registra el estado anterior a tu terraform apply más reciente. Para obtener más información sobre el estado de Terraform, consulta su documento oficial.

Si seguiste los pasos de esta página hasta ahora, el siguiente contenido se encuentra en el archivo terraform.tfstate, con cd_override restante en nuestra infraestructura.

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

En el archivo terraform.tfstate.backup, contiene ambas anulaciones antes del desaprovisionamiento.

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