Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Para qualquer implantação, talvez seja necessário inspecionar ou modificar o arquivo de estado. Por
exemplo, você pode importar ou remover recursos da implantação, o que
é feito modificando o arquivo de estado.
Esta página descreve como trabalhar com o arquivo de estado do Terraform
criado para cada implantação e revisão. Para saber mais sobre o arquivo de estado, consulte Estado.
As instruções desta página pressupõem que você conhece o
Terraform.
Antes de começar
Verifique se você tem as permissões necessárias para trabalhar com o arquivo de estado. O papel
config.admin inclui as permissões necessárias. As permissões
específicas necessárias são:
config.deployments.lock
config.revisions.getState
config.deployments.updateState
config.deployments.unlock
config.deployments.getLock
config.deployments.getState
Verifique se você tem uma cópia local da configuração do Terraform. Essa é a
configuração que corresponde ao arquivo de estado com que você está trabalhando.
A cópia local da configuração permite executar comandos como
terraform refresh ou terraform plan localmente enquanto você modifica o
arquivo de estado.
Para modificar ou inspecionar o arquivo de estado, é necessário bloquear a implantação
e fazer o download dele. Em seguida, você pode modificar ou inspecionar o arquivo de estado.
Depois de modificar o arquivo de estado, faça o upload dele para que o Infra Manager o use na implantação.
Bloquear a implantação
Bloqueie a implantação para evitar mudanças nela enquanto você
muda o arquivo de estado. A implantação precisa estar bloqueada para fazer o
download do arquivo de estado.
Confirme se a configuração (arquivos *.tf) está no mesmo diretório que
o arquivo de estado transferido por download (terraform.tfstate).
Inicialize o Terraform:
terraform init
Se você já tiver inicializado o Terraform, talvez seja necessário inicializar
com a flag de reconfiguração:
terraform init -reconfigure
Trabalhe com o arquivo de estado conforme necessário. Por exemplo, é possível fazer
operações de
inspeção de estado
ou
mutação. Para mais detalhes sobre como trabalhar com o arquivo de estado, consulte
Manipulação do estado do Terraform.
Se você fizer alterações nos arquivos de configuração do Terraform localmente, faça o upload da configuração modificada. Faça upload dessa configuração para o
bucket de armazenamento ou o repositório
git público que você está usando como origem para implantar a configuração.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 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)."]]