So, a recap of what I’ve done:
I have a bucket named mybucket
in AWS. I am trying to import the resource to terraform with the following configuration:
- create a resource in terraform:
resource "aws_s3_bucket" "mybucket" {
bucket = "mybucket"
acl = "private"
}
-
Run a terraform init
which will create an empty state (local state), and then a terraform plan
which correctly says that it’s going to create mybucket
.
-
Now I’m running this:
terraform import aws_s3_bucket.mybucket mybucket
and terraform succeeds in importing the state.
- I run
terraform plan
again and get this super weird error:
Error: Failed to load state: Terraform 0.14.4 does not support state version 4, please update.
Am I missing something here?
LordWire, check what version of Terraform is indicated in your .hcl file and what is set in the workspace. I’ve seen reports that this error occurs when the version of the state file itself doesn’t match the workspace. Not knowing what you have set, try setting the workspace to the latest version or the specific version of TF you have installed; it may be needed to create a new workspace when trying that.
Thanks @larryebaum but I don’t think that’s the reason. But it seems to be on the right track. Let me share some more information (because I realized I haven’t):
- the whole environment is empty. The “terraform init” is a complete init! There is no
.terraform
folder, no hcl
files, nothing. Only a terraform file with one resource that already exists and I’m trying to import it in an empty state.
- I don’t work with workspaces. I’m not saying it’s irrelevant, I’m just saying that I only have one default workspace.
- After the
init
phase, a tfstate gets created which points to version 3
. After the terraform import
step that version 3 becomes version 4.
I hope it’s more clear now. Happy to provide the whole code folder if you want.
I tested it and it works fine. Below are the steps.
- Bucket is created in AWS manually
2.terraform init is run against the code with tf 0.14.4 version . It won’t create state file during init phase.
- Run terraform plan . No tf state at this point as well. Run Terraform import which will create the tf state .tfstate will have “version”: 4,
“terraform_version”: “0.14.4”,.
4.Once imported when you run plan again unless there is difference plan would tell no changes.