Gérer les ressources d'utilisation des services avec Terraform
Ce document explique comment provisionner des ressources Service Usage telles que des forçages de quota à l'aide de Terraform.
Pour gérer les forçages de quota des consommateurs, vous pouvez utiliser le schéma google_service_usage_consumer_quota_override afin de spécifier des ressources dans le fichier de configuration Terraform et d'exécuter des commandes Terraform pour appliquer la modification.
Avant de commencer
Consultez le modèle de quota de service, qui décrit la terminologie utilisée dans ce tutoriel.
Configurez Terraform et les identifiants:
Installez Google Cloud SDK.
Créez vos identifiants par défaut de l'application:
gcloud auth application-default login
Créer des remplacements de quota du client
Créez un fichier de configuration:
Accédez à votre répertoire de travail.
Copiez et collez le contenu suivant dans un nouveau fichier nommé 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 }
Le contenu que vous avez placé dans votre fichier
main.tf
contient les informations de configuration de deux forçages de consommation:"regional_override"
et"cd_override"
. Pour en savoir plus sur chaque champ, consultez la documentation de référence sur les arguments.Initialisez le répertoire de travail.
terraform init -upgrade
Appliquez la configuration Terraform.
terraform apply
Terraform inspecte l'état actuel et le compare aux informations du fichier de configuration. Une fois la comparaison terminée, Terraform imprime une liste des modifications, puis fournit des options pour continuer ou annuler l'application des modifications.
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:
Saisissez yes (Oui) :
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.
Modifier les remplacements de quota du client
- Pour modifier un forçage, modifiez votre fichier
main.tf
et modifiez la valeur d'un champoverride_value
. Appliquez la configuration Terraform.
terraform apply
Supprimer les remplacements de quota du client
Pour supprimer un remplacement de quota du client, supprimez le bloc de ressources correspondant du fichier main.tf. Vous pouvez supprimer plusieurs forçages. Pour supprimer tous les forçages, assurez-vous que votre fichier main.tf est vide.
Appliquez la configuration Terraform.
terraform apply
Par exemple, pour supprimer le forçage régional que vous avez configuré précédemment. Supprimez le bloc faisant référence à la ressource:
resource "google_service_usage_consumer_quota_override" "regional_override" { ... }
Le résultat après
terraform apply
est le suivant: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:
Saisissez yes (oui) pour continuer:
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.
Fichiers d'état
Vous remarquerez peut-être que deux fichiers sont générés dans votre répertoire de travail: terraform.tfstate et terraform.tfstate.backup. Le premier fichier enregistre l'état actuel de votre infrastructure après l'exécution du dernier terraform apply
. Le deuxième fichier enregistre l'état avant votre dernière terraform apply
. Pour en savoir plus sur l'état Terraform, consultez la documentation officielle.
Si vous avez suivi les étapes de cette page jusqu'à présent, le contenu suivant se trouve dans le fichier terraform.tfstate, avec l'cd_override
laissé dans notre 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"
}
]
}
]
}
Dans le fichier terraform.tfstate.backup, les deux forçages sont inclus avant le déprovisionnement.
{
"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"
}
]
}
]
}