Mengelola resource Service Usage dengan Terraform
Dokumen ini menjelaskan cara menyediakan resource Penggunaan Layanan seperti penggantian kuota menggunakan Terraform.
Untuk mengelola penggantian kuota konsumen, Anda dapat menggunakan skema google_service_usage_consumer_quota_override untuk menentukan resource dalam file konfigurasi Terraform dan menjalankan perintah Terraform untuk menerapkan perubahan.
Sebelum memulai
Tinjau model kuota layanan yang menjelaskan terminologi yang digunakan dalam tutorial ini.
Konfigurasikan Terraform dan kredensial:
Instal Google Cloud SDK.
Buat Kredensial Default Aplikasi:
gcloud auth application-default login
Membuat Penggantian Kuota Konsumen
Buat file konfigurasi:
Buka direktori kerja Anda.
Salin dan tempel konten berikut ke file baru bernama 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 }
Konten yang Anda tempatkan di file
main.tf
berisi informasi konfigurasi untuk dua penggantian konsumen:"regional_override"
dan"cd_override"
. Lihat Referensi Argumen untuk penjelasan setiap kolom.Lakukan inisialisasi direktori kerja.
terraform init -upgrade
Terapkan konfigurasi Terraform.
terraform apply
Terraform memeriksa status saat ini dan membandingkannya dengan informasi dalam file konfigurasi. Setelah perbandingan selesai, Terraform mencetak daftar perubahan, lalu memberikan opsi untuk melanjutkan atau membatalkan penerapan perubahan.
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:
Masukkan 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.
Ubah Penggantian Kuota Konsumen
- Untuk mengubah penggantian, edit file
main.tf
dan ubah nilai kolomoverride_value
. Terapkan konfigurasi Terraform.
terraform apply
Hapus Penggantian Kuota Konsumen
Untuk menghapus penggantian kuota konsumen, hapus blok resource yang sesuai dari file main.tf. Anda dapat menghapus beberapa penggantian. Untuk menghapus semua penggantian, pastikan file main.tf kosong.
Terapkan konfigurasi Terraform.
terraform apply
Misalnya, untuk menghapus penggantian regional yang Anda sediakan sebelumnya. Hapus blok yang merujuk ke resource:
resource "google_service_usage_consumer_quota_override" "regional_override" { ... }
Output setelah
terraform apply
adalah sebagai berikut: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:
Masukkan yes untuk melanjutkan:
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.
File Status
Anda mungkin melihat bahwa ada dua file yang dihasilkan di direktori kerja: terraform.tfstate dan terraform.tfstate.backup. File pertama mencatat status infrastruktur saat ini setelah Anda menjalankan terraform apply
terbaru. File kedua merekam status sebelum terraform apply
terbaru. Untuk mengetahui informasi selengkapnya tentang Status Terraform, baca dokumen resmi.
Jika Anda telah mengikuti langkah-langkah di halaman ini sejauh ini, konten berikut ada di file terraform.tfstate, dengan cd_override
yang tersisa di infrastruktur kita.
{
"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"
}
]
}
]
}
Dalam file terraform.tfstate.backup, file ini berisi kedua penggantian sebelum cabut akses.
{
"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"
}
]
}
]
}