One state file for multiple deployments with similar configuration

I want to create 2 deployments with the same configuration but for different clients using the same state file.

Client-1 :
RG : CLIENT1-RG
VM: CLIENT1-VM001

Client-2:
RG: CLIENT2-RG
VM: CLIENT2-VM001

Client-1 deployment was completed successfully and the state file is stored in the backend called “terraform.tfstate” and now I am using the same backend and state file for Client-2 as well.

When I run terraform plan it is trying to destroy the resources created for Client-1 and then it is trying to create the resources for Client-2.

Any reason, why terraform is trying the destroy the previously created resources?

That is expected. The state file & code are linked, so to do what you are wanting you would need two state files.

The standard way to do what you are wanting is to move all the resources into a module and then have two minimal root modules (the provider & backend config code plus a call to the module).

1 Like

However, Terraform doesn’t scale well as the number of resources in a state gets large, so if you intend to support many clients, you should use one state per client instead.

Even if you don’t intend that, it means you can not easily deploy changes to one client’s environment without risk of accidentally affecting another.

1 Like