Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Sie können die Statusdatei für jede Bereitstellung prüfen oder ändern. Beispielsweise können Sie Ressourcen aus der Bereitstellung importieren oder entfernen. Dazu müssen Sie die Statusdatei ändern.
Auf dieser Seite wird beschrieben, wie Sie mit der Terraform-Zustandsdatei arbeiten, die für jede Bereitstellung und Version erstellt wird. Weitere Informationen zur Statusdatei finden Sie unter Status.
Die Anleitung auf dieser Seite setzt voraus, dass Sie mit Terraform vertraut sind.
Hinweis
Prüfen Sie, ob Sie die erforderlichen Berechtigungen zum Arbeiten mit der Statusdatei haben. Die Rolle config.admin enthält die erforderlichen Berechtigungen. Folgende Berechtigungen sind erforderlich:
config.deployments.lock
config.revisions.getState
config.deployments.updateState
config.deployments.unlock
config.deployments.getLock
config.deployments.getState
Sie benötigen eine lokale Kopie der Terraform-Konfiguration. Dies ist die Konfiguration, die der verwendeten Statusdatei entspricht.
Mit der lokalen Kopie der Konfiguration können Sie Befehle wie terraform refresh oder terraform plan lokal ausführen, während Sie die Statusdatei ändern.
Wenn Sie die Statusdatei ändern oder prüfen möchten, müssen Sie die Bereitstellung sperren und die Statusdatei herunterladen. Sie können die Statusdatei dann mutieren oder prüfen.
Nachdem Sie die Statusdatei geändert haben, laden Sie sie hoch, damit sie von Infra Manager für Ihre Bereitstellung verwendet werden kann.
Bereitstellung sperren
Sperren Sie die Bereitstellung, um Änderungen an der Bereitstellung zu verhindern, während Sie die Statusdatei ändern. Die Bereitstellung muss gesperrt sein, damit die Statusdatei heruntergeladen werden kann.
Wenn Sie lokale Änderungen an den Terraform-Konfigurationsdateien vornehmen, laden Sie die geänderte Konfiguration hoch. Laden Sie diese Konfiguration in den Speicher-Bucket oder das öffentliche Git-Repository hoch, das Sie als Quelle für die Bereitstellung der Konfiguration verwenden.
[[["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-08-19 (UTC)."],[[["\u003cp\u003eThis guide outlines how to interact with Terraform state files for deployments, including inspecting and modifying them.\u003c/p\u003e\n"],["\u003cp\u003eBefore working with a state file, you must lock the deployment using the \u003ccode\u003egcloud infra-manager deployments lock\u003c/code\u003e command to prevent changes and to download the state file.\u003c/p\u003e\n"],["\u003cp\u003eThe state file can be downloaded using a signed Cloud Storage URL and the \u003ccode\u003egcloud infra-manager deployments export-statefile\u003c/code\u003e command, and then you can locally initialize Terraform using the command \u003ccode\u003eterraform init\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAfter making local changes to the state file, you need to upload the updated file using a signed Cloud Storage URL and the command \u003ccode\u003egcloud infra-manager deployments import-statefile\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAfter uploading a state file, the deployment must be unlocked with the command \u003ccode\u003egcloud infra-manager deployments unlock\u003c/code\u003e, using the obtained lock ID.\u003c/p\u003e\n"]]],[],null,["# Manage the Terraform state file\n\nFor any deployment, you may want to inspect or modify the state file. For\nexample, you may want to import or remove resources from the deployment, which\nyou do through modifying the state file.\n\nThis page describes how to work with the Terraform state file\nthat is created for each deployment and revision. To learn more about the state\nfile, see [State](https://developer.hashicorp.com/terraform/language/state).\n\nThe instructions on this page assume that you are familiar with\n[Terraform](https://developer.hashicorp.com/terraform/intro).\n\nBefore you begin\n----------------\n\n1. Ensure you have the required permissions to work with the state file. The\n `config.admin` role includes the required permissions. The specific\n permissions needed are:\n\n - `config.deployments.lock`\n - `config.revisions.getState`\n - `config.deployments.updateState`\n - `config.deployments.unlock`\n - `config.deployments.getLock`\n - `config.deployments.getState`\n2. Ensure you have a local copy of the Terraform configuration. This is the\n configuration that corresponds to the state file you are working with.\n\n The local copy of the configuration allows you to run commands such as\n `terraform refresh` or `terraform plan` locally while you are modifying the\n state file.\n3. [Install Terraform](https://developer.hashicorp.com/terraform/downloads)\n to use the Terraform CLI on your local machine.\n\nMutate or inspect the state file\n--------------------------------\n\nTo mutate (modify) or inspect the state file, you need to lock the deployment\nand download the state file. You can then mutate or inspect the state file.\n\nAfter you mutate the state file, you then upload the file for\nInfra Manager to use in your deployment.\n\n### Lock the deployment\n\n1. Lock the deployment to prevent any changes to the deployment while you\n mutate the statefile. The deployment must be locked to be able to\n download the state file.\n\n gcloud infra-manager deployments lock \u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e --project \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e --location \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e\n\n Replace:\n - \u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e with the deployment identifier.\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with the project where the deployment runs.\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e with the location where the deployment runs.\n\n The output of this command contains a `lock ID` that is used for uploading\n and unlocking the statefile.\n2. To retrieve the lock ID at any time, use the command:\n\n gcloud infra-manager deployments export-lock \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDEPLOYMENT_ID\u003c/span\u003e\u003c/var\u003e --project \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e --location \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eLOCATION\u003c/span\u003e\u003c/var\u003e\n\n### Download the state file\n\nTo download the state file, you use a\n[signed Cloud Storage URL](/storage/docs/access-control/signed-urls): \n\n SIGNED_STATE_DOWNLOAD_URL=$(gcloud infra-manager deployments export-statefile \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDEPLOYMENT_ID\u003c/span\u003e\u003c/var\u003e --project \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e --location \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eLOCATION\u003c/span\u003e\u003c/var\u003e --format=\"get(signedUri)\")\n\n curl -s -X GET --output terraform.tfstate ${SIGNED_STATE_DOWNLOAD_URL}\n\n### Mutate the state file locally\n\n1. Confirm that the configuration (`*.tf` files) is in the same directory as\n the downloaded state file (`terraform.tfstate`).\n\n2. Initialize Terraform:\n\n terraform init\n\n3. If you have previously initialized Terraform, you may need to initialize\n with the reconfigure flag:\n\n terraform init -reconfigure\n\n4. Work with the state file as you need. For example, you can do\n [state inspection](https://developer.hashicorp.com/terraform/cli/state/inspect)\n or\n [mutation](https://developer.hashicorp.com/terraform/cli/state/move)\n operations. For more details about working with the state file, see\n [Manipulating Terraform State](https://developer.hashicorp.com/terraform/cli/state).\n\n5. If you make any changes to the Terraform configuration files locally, then\n upload the modified configuration. Upload this configuration to the\n [storage bucket](/infrastructure-manager/docs/update-deployment) or public\n git repository that you are using as the source to deploy the configuration.\n\n### Upload the state file\n\nUse a [signed Cloud Storage URL](/storage/docs/access-control/signed-url)\nto upload the state file:\n\n1. Get the lock ID:\n\n LOCK_ID=$(gcloud infra-manager deployments export-lock \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDEPLOYMENT_ID\u003c/span\u003e\u003c/var\u003e --project \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e --location \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eLOCATION\u003c/span\u003e\u003c/var\u003e --format=\"get(lockId)\")\n\n2. Get the upload URL:\n\n SIGNED_STATE_UPLOAD_URL=$(gcloud infra-manager deployments import-statefile \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDEPLOYMENT_ID\u003c/span\u003e\u003c/var\u003e --project \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e --location \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eLOCATION\u003c/span\u003e\u003c/var\u003e --lock-id ${LOCK_ID} --format=\"get(signedUri)\")\n\n curl -s -X PUT --upload-file terraform.tfstate $SIGNED_STATE_UPLOAD_URL\n\n### Unlock the deployment\n\n1. Get the lock ID:\n\n LOCK_ID=$(gcloud infra-manager deployments export-lock \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDEPLOYMENT_ID\u003c/span\u003e\u003c/var\u003e --project \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e --location \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eLOCATION\u003c/span\u003e\u003c/var\u003e --format=\"get(lockId)\")\n\n2. Unlock the deployment:\n\n gcloud infra-manager deployments unlock \u003cvar translate=\"no\"\u003eDEPLOYMENT_ID\u003c/var\u003e --project \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e --location \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e --lock-id ${LOCK_ID}\n\nWhat's next\n-----------\n\n- Learn more about [Terraform with Google Cloud](/docs/terraform).\n- [Update a deployment](/infrastructure-manager/docs/update-deployment).\n- [View the state of a deployment](/infrastructure-manager/docs/view-deployments).\n- [View resources deployed](/infrastructure-manager/docs/view-resources).\n- [Delete a deployment](/infrastructure-manager/docs/delete-deployments)."]]