Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Untuk setiap deployment, Anda dapat memeriksa atau mengubah file status. Misalnya, Anda mungkin ingin mengimpor atau menghapus resource dari deployment, yang Anda lakukan melalui perubahan file status.
Halaman ini menjelaskan cara menggunakan file status Terraform
yang dibuat untuk setiap deployment dan revisi. Untuk mempelajari file
status lebih lanjut, lihat Status.
Petunjuk di halaman ini mengasumsikan bahwa Anda sudah memahami Terraform.
Sebelum memulai
Pastikan Anda memiliki izin yang diperlukan untuk menggunakan file status. Peran
config.admin menyertakan izin yang diperlukan. Izin
tertentu yang diperlukan adalah:
config.deployments.lock
config.revisions.getState
config.deployments.updateState
config.deployments.unlock
config.deployments.getLock
config.deployments.getState
Pastikan Anda memiliki salinan lokal konfigurasi Terraform. Ini adalah konfigurasi yang sesuai dengan file status yang Anda gunakan.
Salinan lokal konfigurasi memungkinkan Anda menjalankan perintah seperti
terraform refresh atau terraform plan secara lokal saat Anda mengubah
file status.
Instal Terraform untuk menggunakan Terraform CLI di komputer lokal Anda.
Mengubah atau memeriksa file status
Untuk mengubah (memodifikasi) atau memeriksa file status, Anda perlu mengunci deployment
dan mendownload file status. Kemudian, Anda dapat mengubah atau memeriksa file status.
Setelah mengubah file status, Anda harus mengupload file tersebut agar
Infra Manager dapat menggunakannya dalam deployment.
Mengunci deployment
Kunci deployment untuk mencegah perubahan pada deployment saat Anda
memutar file status. Deployment harus dikunci agar dapat
mendownload file status.
Pastikan konfigurasi (file *.tf) berada di direktori yang sama dengan file status yang didownload (terraform.tfstate).
Lakukan inisialisasi Terraform:
terraform init
Jika sebelumnya telah melakukan inisialisasi Terraform, Anda mungkin perlu melakukan inisialisasi dengan flag konfigurasi ulang:
terraform init -reconfigure
Gunakan file status sesuai kebutuhan Anda. Misalnya, Anda dapat melakukan operasi
pemeriksaan status
atau
mutasi. Untuk mengetahui detail selengkapnya tentang cara menggunakan file status, lihat
Memanipulasi Status Terraform.
Jika Anda membuat perubahan pada file konfigurasi Terraform secara lokal, upload konfigurasi yang diubah. Upload konfigurasi ini ke
bucket penyimpanan atau repositori git publik
yang Anda gunakan sebagai sumber untuk men-deploy konfigurasi.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]