Legen Sie das Google Cloud-Standardprojekt fest, auf das Sie Ihre Terraform-Konfigurationen anwenden möchten.
Sie müssen diesen Befehl nur einmal pro Projekt und in jedem beliebigen Verzeichnis ausführen.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Umgebungsvariablen werden überschrieben, wenn Sie in der Terraform-Konfigurationsdatei explizite Werte festlegen.
Verzeichnis vorbereiten
Jede Terraform-Konfigurationsdatei muss ein eigenes Verzeichnis haben (auch als Stammmodul bezeichnet).
Erstellen Sie in Cloud Shell ein Verzeichnis und eine neue Datei in diesem Verzeichnis. Der Dateiname muss die Erweiterung .tf haben, z. B. main.tf. In dieser Anleitung wird die Datei als main.tf bezeichnet.
mkdir DIRECTORY && cd DIRECTORY && touch main.tf
Wenn Sie einer Anleitung folgen, können Sie den Beispielcode in jedem Abschnitt oder Schritt kopieren.
Kopieren Sie den Beispielcode in das neu erstellte main.tf.
Kopieren Sie optional den Code aus GitHub. Dies wird empfohlen, wenn das Terraform-Snippet Teil einer End-to-End-Lösung ist.
Prüfen und ändern Sie die Beispielparameter, die auf Ihre Umgebung angewendet werden sollen.
Speichern Sie die Änderungen.
Initialisieren Sie Terraform. Dies ist nur einmal für jedes Verzeichnis erforderlich.
terraform init
Fügen Sie optional die Option -upgrade ein, um die neueste Google-Anbieterversion zu verwenden:
terraform init -upgrade
Änderungen anwenden
Prüfen Sie die Konfiguration und prüfen Sie, ob die Ressourcen, die Terraform erstellen oder aktualisieren wird, Ihren Erwartungen entsprechen:
terraform plan
Korrigieren Sie die Konfiguration nach Bedarf.
Wenden Sie die Terraform-Konfiguration an. Führen Sie dazu den folgenden Befehl aus und geben Sie yes an der Eingabeaufforderung ein:
terraform apply
Warten Sie, bis Terraform die Meldung „Apply complete“ anzeigt.
Öffnen Sie Ihr Google Cloud-Projekt, um die Ergebnisse aufzurufen. Rufen Sie in der Google Cloud Console Ihre Ressourcen in der Benutzeroberfläche auf, um sicherzustellen, dass Terraform sie erstellt oder aktualisiert hat.
Neu formatieren
Geben Sie den folgenden Befehl ein, um Ihre Terraform-Konfiguration im Standardstil neu zu formatieren:
terraform fmt
Validieren
Geben Sie den folgenden Befehl ein, um zu prüfen, ob Ihre Konfiguration gültig ist:
terraform validate
Änderungen löschen
Entfernen Sie Ressourcen, die zuvor mit Ihrer Terraform-Konfiguration angewendet wurden, indem Sie den folgenden Befehl ausführen und yes an der Eingabeaufforderung eingeben:
terraform destroy
Projekt-ID angeben
Wenn Sie den Befehl export GOOGLE_CLOUD_PROJECT ausführen, können die meisten Ressourcen die project_id ableiten.
Bei einigen Ressourcen wie project_iam_* kann die Projekt-ID nicht abgeleitet werden. Zur Umgehung dieses Problems wird in einigen Beispielen die Datenquelle data "google_project" verwendet. Sie können diese Datenquelle durch den String der Projekt-ID oder eine Variable ersetzen.
[[["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: 2025-04-21 (UTC)."],[[["\u003cp\u003eTerraform configurations are applied within a designated Google Cloud project, initiated by setting the \u003ccode\u003eGOOGLE_CLOUD_PROJECT\u003c/code\u003e environment variable in Cloud Shell.\u003c/p\u003e\n"],["\u003cp\u003eEach Terraform configuration requires its own directory, where you will need to create a \u003ccode\u003e.tf\u003c/code\u003e file, such as \u003ccode\u003emain.tf\u003c/code\u003e, and then initialize Terraform using the \u003ccode\u003eterraform init\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eBefore applying changes, you must use the \u003ccode\u003eterraform plan\u003c/code\u003e command to review the planned resource changes and verify that the resources Terraform is going to create or update match your expectations.\u003c/p\u003e\n"],["\u003cp\u003eTo apply changes to your Google Cloud project, use the \u003ccode\u003eterraform apply\u003c/code\u003e command and confirm the action, or use \u003ccode\u003eterraform destroy\u003c/code\u003e to remove previously applied resources.\u003c/p\u003e\n"],["\u003cp\u003eYou can utilize the \u003ccode\u003eterraform fmt\u003c/code\u003e and \u003ccode\u003eterraform validate\u003c/code\u003e commands to reformat and check if your Terraform configuration is valid, respectively.\u003c/p\u003e\n"]]],[],null,["# Basic Terraform commands\n\nTo apply your Terraform configuration in a Google Cloud project, complete the steps in the\nfollowing sections.\n\nPrepare Cloud Shell\n-------------------\n\n1. Launch [Cloud Shell](https://shell.cloud.google.com/).\n2. Set the default Google Cloud project\n where you want to apply your Terraform configurations.\n\n You only need to run this command once per project, and you can run it in any directory. \n\n ```\n export GOOGLE_CLOUD_PROJECT=PROJECT_ID\n ```\n\n Environment variables are overridden if you set explicit values in the Terraform\n configuration file.\n\nPrepare the directory\n---------------------\n\nEach Terraform configuration file must have its own directory (also\ncalled a *root module*).\n\n1. In [Cloud Shell](https://shell.cloud.google.com/), create a directory and a new file within that directory. The filename must have the `.tf` extension---for example `main.tf`. In this tutorial, the file is referred to as `main.tf`. \n\n ```\n mkdir DIRECTORY && cd DIRECTORY && touch main.tf\n ```\n2. If you are following a tutorial, you can copy the sample code in each section or step.\n\n Copy the sample code into the newly created `main.tf`.\n\n Optionally, copy the code from GitHub. This is recommended\n when the Terraform snippet is part of an end-to-end solution.\n3. Review and modify the sample parameters to apply to your environment.\n4. Save your changes.\n5. Initialize Terraform. You only need to do this once per directory. \n\n ```\n terraform init\n ```\n\n Optionally, to use the latest Google provider version, include the `-upgrade`\n option: \n\n ```\n terraform init -upgrade\n ```\n\nApply the changes\n-----------------\n\n1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations: \n\n ```\n terraform plan\n ```\n\n Make corrections to the configuration as necessary.\n2. Apply the Terraform configuration by running the following command and entering `yes` at the prompt: \n\n ```\n terraform apply\n ```\n\n Wait until Terraform displays the \"Apply complete!\" message.\n3. [Open your Google Cloud project](https://console.cloud.google.com/) to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.\n\n| **Note:** Terraform samples typically assume that the required APIs are enabled in your Google Cloud project.\n\nReformat\n--------\n\nTo reformat your Terraform configuration in the standard style, enter the\nfollowing command: \n\n```\nterraform fmt\n```\n\nValidate\n--------\n\nTo check whether your configuration is valid, enter the following command: \n\n```\nterraform validate\n```\n\nDelete changes\n--------------\n\nRemove resources previously applied with your Terraform configuration by running the following\ncommand and entering `yes` at the prompt: \n\n```\nterraform destroy\n```\n\nSpecify the project ID\n----------------------\n\nIf you run the `export GOOGLE_CLOUD_PROJECT` command, most resources can infer\nthe `project_id`.\n\nSome resources, such as `project_iam_*`, cannot infer the project ID. As a\nworkaround, some samples use the [`data \"google_project\"`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project)\ndata source. You can replace this data source with the project ID string or a\nvariable.\n\nFor a sample that uses this workaround, see\n[sql_instance_iam_condition](https://github.com/terraform-google-modules/terraform-docs-samples/blob/main/cloud_sql/instance_iam_condition/main.tf).\n\nWhat's next\n-----------\n\n- [Learn more about Terraform's CLI features](https://www.terraform.io/cli/commands)."]]