Stay organized with collections
Save and categorize content based on your preferences.
For any deployment, you may want to inspect or modify the state file. For
example, you may want to import or remove resources from the deployment, which
you do through modifying the state file.
This page describes how to work with the Terraform state file
that is created for each deployment and revision. To learn more about the state
file, see State.
The instructions on this page assume that you are familiar with
Terraform.
Before you begin
Ensure you have the required permissions to work with the state file. The
config.admin role includes the required permissions. The specific
permissions needed are:
config.deployments.lock
config.revisions.getState
config.deployments.updateState
config.deployments.unlock
config.deployments.getLock
config.deployments.getState
Ensure you have a local copy of the Terraform configuration. This is the
configuration that corresponds to the state file you are working with.
The local copy of the configuration allows you to run commands such as
terraform refresh or terraform plan locally while you are modifying the
state file.
To mutate (modify) or inspect the state file, you need to lock the deployment
and download the state file. You can then mutate or inspect the state file.
After you mutate the state file, you then upload the file for
Infra Manager to use in your deployment.
Lock the deployment
Lock the deployment to prevent any changes to the deployment while you
mutate the statefile. The deployment must be locked to be able to
download the state file.
If you make any changes to the Terraform configuration files locally, then
upload the modified configuration. Upload this configuration to the
storage bucket or public
git repository that you are using as the source to deploy the configuration.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 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)."]]