使用 Terraform 管理 Service Usage 资源
本文档介绍如何使用 Terraform 预配服务使用资源,例如配额覆盖。
要管理使用方配额替换值,您可以使用 google_service_usage_consumer_quota_override 架构来指定 Terraform 配置文件中的资源并运行 Terraform 命令来应用更改。
准备工作
查看服务配额模型,其中介绍了本教程中使用的术语。
配置 Terraform 和凭据:
创建使用方配额替换值
创建配置文件:
转到您的工作目录。
将以下内容复制并粘贴到名为 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
文件中的内容包含两个使用方替换项的配置信息:"regional_override"
和"cd_override"
。有关每个字段的说明,请参阅参数参考。初始化工作目录。
terraform init -upgrade
应用 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:
输入 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.
修改使用方配额替换
- 如需修改替换值,请修改
main.tf
文件并更改override_value
字段的值。 应用 Terraform 配置。
terraform apply
删除使用方配额替换值
如需删除使用方配额替换值,请从 main.tf 文件中移除相应的资源块。您可以移除多个替换值。如需移除所有覆盖,请确保 main.tf 文件为空。
应用 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:
输入 yes 以继续:
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。在您执行最新的 terraform apply
后,第一个文件会记录您的基础架构的当前状态。第二个文件会记录最新 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"
}
]
}
]
}