How To Manually Edit A Terraform Cloud State File
Sometimes it's just easier to edit the Terraform state file, here's how you can do it and save the changes back to Terraform Cloud
There are rare occasions where you will be required to manually edit a Terraform state file. One such scenario would be if you manually delete Azure resources through the Azure GUI and subsequent terraform apply commands also attempt to delete the already-deleted resources (and fails).
In this case, a Terraform Cloud state refresh will not pick up that the "missing" resources have already been deleted, and future terraform apply commands will continue to fail.
Your options are to either execute terraform state rm resource.name, or manually remove the resource from the Terraform state file.
Unfortunately, Terraform Cloud does not offer a native mechanism to edit state files, but we can still do it manually.
In short, the procedure involves:
- Downloading the newest Terraform Cloud state file to your local machine
- Locally manipulating the downloaded Terraform Cloud
.tfstatefile - Reconfiguring the Terraform Cloud Workspace's
main.tfon your local machine to use thebackend "remote" {}backend instead of thecloud {}backend - Initializing the Terraform code on your local machine
- Pushing the manipulated Terraform Cloud state file back to Terraform Cloud
- Restoring your original backend and re-initializing it, locally
.terraform/terraform.tfstate because terraform state push will treat it as a normal state file and will fail to upload due to Terraform Cloud using state version 4 whereas the Terraform CLI only supports state versions <= 3Procedure
-
Ensure you have a copy of the Terraform code downloaded to your machine
-
On your workstation, delete the
.terraform/andterraform.tfstate.d/folders if they exist (they will only exist if you've previously runterraformcommands in the stack). -
In your main terraform block (I like to use
providers.tf), temporarily comment out thecloud {}code block.terraform:providers.tf terraform { # cloud { # hostname = "app.terraform.io" # organization = "my_organization" # workspaces { # tags = ["my_app"] # } # } }
Manually editing Terraform Cloud state file
-
Still in
providers.tf, configure abackend "remote" {}code block inside the mainterraform {}code block.terraform:providers.tf terraform { # cloud { # hostname = "app.terraform.io" # organization = "my_organization" # workspaces { # tags = ["my_app"] # } # } backend "remote" { hostname = "app.terraform.io" organization = "my_organization" workspaces { name = "my_workspace_name" } } } -
Execute on your machine:
terraform init -reconfigure -
In Terraform Cloud, go to your Workspace -> State -> Download the latest state file and save it to
/local/path/to/repo/ -
Manipulate the downloaded state file as needed.
-
Upload the modified
.tfstatefile to Terraform Cloud by executing:terraform state push /local/path/to/repo/ filename.tfstateTroubleshooting: If you get the error "Failed to write state: cannot overwrite existing state with serial number with a different state that has the same serial", simply increment the"serial"number at the top of the downloaded.tfstatefile and retryterraform state push -
Remove the temporary
backend "remote" {}code block fromproviders.tfand uncomment the originalcloud {}code block:terraform terraform { cloud { hostname = "app.terraform.io" organization = "my_organization" workspaces { tags = ["my_app"] } } } -
Execute on your machine:
terraform init -reconfigure