[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\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)."]]