Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Pour tout déploiement, vous pouvez inspecter ou modifier le fichier d'état. Par exemple, vous pouvez importer ou supprimer des ressources du déploiement, ce que vous faites en modifiant le fichier d'état.
Cette page explique comment utiliser le fichier d'état Terraform créé pour chaque déploiement et chaque révision. Pour en savoir plus sur le fichier d'état, consultez la section État.
Les instructions de cette page partent du principe que vous connaissez bien Terraform.
Avant de commencer
Assurez-vous de disposer des autorisations requises pour utiliser le fichier d'état. Le rôle config.admin inclut les autorisations requises. Les autorisations spécifiques requises sont les suivantes:
config.deployments.lock
config.revisions.getState
config.deployments.updateState
config.deployments.unlock
config.deployments.getLock
config.deployments.getState
Assurez-vous de disposer d'une copie locale de la configuration Terraform. Il s'agit de la configuration qui correspond au fichier d'état avec lequel vous travaillez.
La copie locale de la configuration vous permet d'exécuter des commandes telles que terraform refresh ou terraform plan localement lorsque vous modifiez le fichier d'état.
Installez Terraform pour utiliser la CLI Terraform sur votre machine locale.
Muter ou inspecter le fichier d'état
Pour muter (modifier) ou inspecter le fichier d'état, vous devez verrouiller le déploiement et télécharger le fichier d'état. Vous pouvez ensuite modifier ou inspecter le fichier d'état.
Après avoir modifié le fichier d'état, vous l'importez pour qu'Infra Manager l'utilise dans votre déploiement.
Verrouiller le déploiement
Verrouillez le déploiement pour empêcher toute modification du déploiement pendant que vous modifiez le fichier d'état. Le déploiement doit être verrouillé pour pouvoir télécharger le fichier d'état.
Vérifiez que la configuration (fichiers *.tf) se trouve dans le même répertoire que le fichier d'état téléchargé (terraform.tfstate).
Initialisez Terraform :
terraform init
Si vous avez déjà initialisé Terraform, vous devrez peut-être l'initialiser avec l'option de reconfiguration:
terraform init -reconfigure
Utilisez le fichier d'état selon vos besoins. Par exemple, vous pouvez effectuer des opérations d'inspection de l'état ou de mutation. Pour en savoir plus sur l'utilisation du fichier d'état, consultez la section Manipuler l'état Terraform.
Si vous apportez des modifications aux fichiers de configuration Terraform localement, importez la configuration modifiée. Importez cette configuration dans le bucket de stockage ou le dépôt git public que vous utilisez comme source pour déployer la configuration.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (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)."]]