Terraform Remote StateFile for multiple instances of same configuration and Terraform design

626 views Asked by At

I have a requirement to create a set of a same number of instances for each of our customers. Let us assume my customers Walmart, Apple, and eBay. Each customer has same number of Instances but configuration(subnet, number of disk and disk sizes, OS Images) changes. So I created a variable file for each customer and used it to launch the instances. Until now everything is working as expected. I am able to launch instances for each customer with their own configuration and also able to modify instances as required.

Now, I am trying to move statefile from my local to remote. In backend.tf how do I take the key as dynamic? Upon investigating backed.tf can't accept any interpolations (variables) as it is loaded much before than the core of Terraform can be initialized.

  1. How do I solve this problem? I tried the below approach but I have to run init every time I switch customers.

terraform init --backend-config "key=apple.tfstate"

  1. Is there any fundamental issue with the approach I followed to create instances using a separate variable file?

  2. Do I need to use a single state file for all customers? if so I have 40+ customers, having all of the customers in a single file might be risky/bulky/slow right?

I am not using Terraform Enterprise or Terraform Cloud. Please let me know how to proceed. Thanks!

I use the below command to apply terraform changes

terraform apply -var-file=customers/apple.tfvars -state=statefile/apple.tfstate
terraform apply -var-file=customers/walmart.tfvars -state=statefile/walmart.tfstate

Folder structure

  ├── main.tf
  └── variables.tf
  ├── gcp_compute_disk
  │   ├── gcp_compute_disk.tf
  │   └── variables.tf
  ├── gcp_instance
  │   ├── gcp_instance.tf
  │   └── variables.tf
  ├── customers
  │   ├── apple.tfvars
  │   ├── ebay.tfvars
  │   ├── walmart.tfvars
  ├── statefile
  │   ├── apple.tfstate
  │   ├── ebay.tfstate
  │   ├── walmart.tfstate


0

There are 0 answers