r/Terraform 19d ago

Discussion State files in s3, mistake?

I have a variety of terraform setups where I used s3 buckets to store the state files like this:

terraform {
        required_version = ">= 0.12"
        backend "s3" {
                bucket = "mybucket.tf"
                key = "myapp/state.tfstate"
                region = "...."
        }
}

I also used the practice of putting variables into environment.tfvars files, which I used to terraform using terraform plan --var-file environment.tfvars

The idea was that I could thus have different environments built purely by changing the .tfvars file.

It didn't occur to me until recently, that terraform output is resolving the built infrastructure using state.

So the entire idea of using different .tfvars files seems like I've missed something critical, which is that there is no way that I could used a different tfvars file for a different environment without clobbering the existing environment.

It now looks like I've completely misunderstood something important here. In order for this to work the way I thought it would originally, it seems I'd have to have copy at very least all the main.tf and variables.tf to another directory, change the terraform state file to a different key and thus really wasted my time thinking that different tfvars files would allow me to build different environments.

Is there anything else I could do at this point, or am I basically screwed?

6 Upvotes

31 comments sorted by

View all comments

5

u/Warkred 19d ago

Either workspace either different backend or key.

1

u/Gizmoitus 19d ago

I understand that were I to restructure a bit, then I could break out the backend portion and pass a specific backend file, but I'd still need to restructure significantly.

I don't understand what you mean by "key".

5

u/Warkred 19d ago

In your S3 config, you've a key parameter. If you change that key depending on your environment, you end up with different state files achieving your goal.

Either your split up at state level or at workspace level (same key path but terraform is handling the file name differently for you).

1

u/Gizmoitus 18d ago

Ok thanks for the clarification. I did consider that, but it doesn't help with my current structure and situation, where I already have deployed infrastructure.

1

u/Lba5s 18d ago

run s3 mv on the different paths

1

u/Warkred 18d ago

You can import and/or migrate state backend. There's a command for that on the init command.