简体   繁体   中英

Error when manually moving terraform state file and running terraform plan

I have recently been asked to take over management of VPC infrastructure on AWS. This infrastructure is currently being managed by another department; they use terraform and have configured the backend for the state file on Azure.

Since I do not have any kind of access to Azure, I asked them to share the terraform configuration code and send me the terraform.tfstate file stored in their backend.

My idea for the first few steps in taking over this project was:

  1. Copy the configuration files locally and remove the part of the code that configures the backend to Azure (so that terraform considers a local file).
  2. Run terraform init . At this point, here's a quick example of my local folder layout for the project:
L project
    .terraform/
    main.tf
  1. Place the original terraform.tfstate inside the .terraform folder.
  2. Run terraform plan and expect no changes in infrastructure.

What happens at step 4 is I get this error:

Error: Failed to load state: Terraform 1.1.9 does not support state version 4, please update.

When inspecting the state file, I see "terraform_version": "1.0.2" which confirms that our terraform versions differ.

Note: if I instead do not copy that state file into the directory, then the terraform plan command tells me that I will be creating a bunch of new resources (as expected, as it has no state file to read from).

What is very strange though, is that if I place the terraform.tfstate in the root directory where I'm running my terraform commands from, so inside project/ instead of .terraform/ , and run terraform plan , I don't get the error message and terraform tells me there's nothing to update. Therefore, terraform seems to be reading the state file correctly when I place it in the incorrect location.

Could anyone help me figure out what's going on here? I don't want to proceed with terraform apply until I am sure I am not risking anything here.

What is very strange though, is that if I place the terraform.tfstate in the root directory where I'm running my terraform commands from, so inside project/ instead of.terraform/, and run terraform plan, I don't get the error message and terraform tells me there's nothing to update. Therefore, terraform seems to be reading the state file correctly when I place it in the incorrect location.

local terraform state is correctly stored in the root folder, not inside .terraform/ subfolder. What you're describing as incorrect location for tfstate is in fact the correct location for tfstate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM