Praktik terbaik saat menggunakan resource Google Cloud
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Praktik terbaik untuk menyediakan resource Google Cloud dengan Terraform, terintegrasi dalam modul Cloud Foundation Toolkit yang
dikelola oleh Google. Dokumen ini mengulang beberapa praktik terbaik tersebut.
Panduan ini bukan merupakan pengantar untuk Terraform. Untuk pengantar penggunaan Terraform dengan Google Cloud, lihat Mulai menggunakan Terraform.
Menyimulasikan image virtual machine
Secara umum, sebaiknya Anda membuat image virtual machine
menggunakan alat seperti Packer.
Terraform kemudian hanya perlu meluncurkan mesin menggunakan gambar yang telah dibuat sebelumnya.
Jika gambar yang dibuat sebelumnya tidak tersedia, Terraform dapat menyerahkan virtual
machine baru ke alat manajemen konfigurasi dengan blok provisioner. Sebaiknya
hindari metode ini dan hanya gunakan sebagai
upaya terakhir.
Untuk membersihkan status lama yang terkait dengan instance, penyedia yang memerlukan
logika penghapusan harus menggunakan blok provisioner dengan when = destroy.
Terraform harus memberikan informasi konfigurasi VM ke manajemen
konfigurasi dengan
metadata instance.
Mengelola Identity and Access Management
Saat menyediakan asosiasi IAM dengan Terraform, tersedia
beberapa resource yang berbeda:
google_*_iam_policy dan google_*_iam_binding membuat asosiasi IAM otoritatif,
dengan resource Terraform berfungsi sebagai satu-satunya
sumber tepercaya untuk izin yang dapat ditetapkan ke resource yang relevan.
Jika izin berubah di luar Terraform, Terraform pada eksekusi
berikutnya akan menimpa semua izin untuk mewakili kebijakan seperti yang ditetapkan dalam konfigurasi
Anda. Hal ini mungkin masuk akal untuk resource yang sepenuhnya dikelola oleh
konfigurasi Terraform tertentu, tetapi hal ini berarti peran yang
dikelola secara otomatis oleh Google Cloud akan dihapus, yang berpotensi mengganggu
fungsionalitas beberapa layanan.
Untuk mencegah hal ini, sebaiknya gunakan resource google_*_iam_member
secara langsung atau
modul IAM dari Google.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eThis guide reiterates best practices for provisioning Google Cloud resources with Terraform, as integrated into the Cloud Foundation Toolkit modules.\u003c/p\u003e\n"],["\u003cp\u003eIt's recommended to use pre-baked virtual machine images with tools like Packer, and only utilize Terraform provisioners as a last resort.\u003c/p\u003e\n"],["\u003cp\u003eTerraform should provide VM configuration information to configuration management through instance metadata.\u003c/p\u003e\n"],["\u003cp\u003eWhen managing IAM, using \u003ccode\u003egoogle_*_iam_member\u003c/code\u003e resources or the Google IAM module is preferable to prevent overwriting automatically managed roles.\u003c/p\u003e\n"],["\u003cp\u003eAuthoritative IAM resources like \u003ccode\u003egoogle_*_iam_policy\u003c/code\u003e and \u003ccode\u003egoogle_*_iam_binding\u003c/code\u003e are not ideal for resources that have roles that are automatically managed by Google Cloud.\u003c/p\u003e\n"]]],[],null,["# Best practices when working with Google Cloud resources\n\nBest practices for provisioning Google Cloud resources with Terraform, are\nintegrated into the [Cloud Foundation Toolkit](/foundation-toolkit) modules that\nGoogle maintains. This document reiterates some of these best practices.\n\nThis guide is not an introduction to Terraform. For an introduction to using\nTerraform with Google Cloud, see\n[Get started with Terraform](/docs/terraform/get-started-with-terraform).\n\nBake virtual machine images\n---------------------------\n\nIn general, we recommend that you *bake* virtual machine images\n[using a tool like Packer](/compute/docs/images/image-management-best-practices#automated_baking).\nTerraform then only needs to launch machines using the pre-baked images.\n\nIf pre-baked images are not available, Terraform can hand off new virtual\nmachines to a configuration management tool with a `provisioner` block. We\nrecommend that you avoid this method and only use it as a\n[last resort](https://www.terraform.io/language/resources/provisioners/syntax#provisioners-are-a-last-resort).\nTo clean up old state associated with the instance, provisioners that require\nteardown logic should use a `provisioner` block with `when = destroy`.\n\nTerraform should provide VM configuration information to configuration\nmanagement with\n[instance metadata](/compute/docs/metadata/overview).\n\nManage Identity and Access Management\n-------------------------------------\n\nWhen provisioning IAM associations with Terraform, several\ndifferent resources are available:\n\n- `google_*_iam_policy` (for example, `google_project_iam_policy`)\n- `google_*_iam_binding` (for example, `google_project_iam_binding`)\n- `google_*_iam_member` (for example, `google_project_iam_member`)\n\n`google_*_iam_policy` and `google_*_iam_binding` create *authoritative*\nIAM associations, where the Terraform resources serve as the only\nsource of truth for what permissions can be assigned to the relevant resource.\n\nIf the permissions change outside of Terraform, Terraform on its next\nexecution overwrites all permissions to represent the policy as defined in your\nconfiguration. This might make sense for resources that are wholly managed by a\nparticular Terraform configuration, but it means that roles that are\nautomatically managed by Google Cloud are removed---potentially disrupting\nthe functionality of some services.\n\nTo prevent this, we recommend using either `google_*_iam_member` resources\ndirectly or the\n[IAM module from Google](https://github.com/terraform-google-modules/terraform-google-iam).\n\nWhat's next\n-----------\n\n- Learn about [best practices for version control](/docs/terraform/best-practices/version-control).\n- Learn about [best practices for Terraform operations](/docs/terraform/best-practices/operations)."]]