Best Practices für die Arbeit mit Google Cloud-Ressourcen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Best Practices für die Bereitstellung von Google Cloud-Ressourcen mit Terraform sind in die von Google verwalteten Module des Cloud Foundation Toolkit eingebunden. In diesem Dokument werden einige dieser Best Practices wiederholt.
Dieser Leitfaden ist keine Einführung in Terraform. Eine Einführung in die Verwendung von Terraform mit Google Cloud finden Sie unter Erste Schritte mit Terraform.
VM-Images erstellen
Im Allgemeinen empfehlen wir das Erstellen von VM-Images mit einem Tool wie Packer.
Terraform muss dann nur Maschinen mit den vorgefertigten Images starten.
Wenn keine vorgefertigten Images verfügbar sind, kann Terraform neue virtuelle Maschinen mit einem provisioner-Block an ein Konfigurationsverwaltungstool übergeben. Wir empfehlen, diese Methode zu vermeiden und sie nur als letztes Mittel zu verwenden.
Wenn Sie den mit der Instanz verknüpften alten Zustand bereinigen möchten, sollten Bereitsteller, die eine Bereinigungslogik benötigen, einen provisioner-Block mit when = destroy verwenden.
Terraform sollte VM-Konfigurationsinformationen für die Konfigurationsverwaltung mit Instanzmetadaten bereitstellen.
Identity and Access Management verwalten
Bei der Bereitstellung von IAM-Verknüpfungen mit Terraform sind mehrere verschiedene Ressourcen verfügbar:
google_*_iam_policy (z. B. google_project_iam_policy)
google_*_iam_binding (z. B. google_project_iam_binding)
google_*_iam_member (z. B. google_project_iam_member)
google_*_iam_policy und google_*_iam_binding erstellen autoritative IAM-Verknüpfungen, wobei die Terraform-Ressourcen als einzige „Source of Truth“ dafür dienen, welche Berechtigungen entsprechenden Ressource zugewiesen werden können.
Wenn sich die Berechtigungen außerhalb von Terraform ändern, überschreibt Terraform bei der nächsten Ausführung alle Berechtigungen, um die in Ihrer Konfiguration definierte Richtlinie darzustellen. Dies kann für Ressourcen sinnvoll sein, die vollständig mit einer bestimmten Terraform-Konfiguration verwaltet werden. Dies bedeutet jedoch, dass automatisch von Google Cloud verwaltete Rollen entfernt werden, was die Funktionalität einiger Dienste beeinträchtigen kann.
Um dies zu verhindern, empfehlen wir entweder die direkte Verwendung von google_*_iam_member-Ressourcen oder das IAM-Modul von Google.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2024-12-22 (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)."]]