title: Terraform Remote StateFile for multiple instances of same configuration and Terraform design
body: <p>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.</p>
  <p>Now, I am trying to move statefile from my local to remote. In <code>backend.tf</code> how do I take the <code>key</code> as dynamic? Upon investigating <code>backed.tf</code> can't accept any interpolations (variables) as it is loaded much before than the core of Terraform can be initialized.</p>
  <ol>
  <li>How do I solve this problem? I tried the below approach but I have to run <code>init</code> every time I switch customers.</li>
  </ol>
  <p><code>terraform init --backend-config &quot;key=apple.tfstate&quot;</code></p>
  <ol start="2">
  <li><p>Is there any fundamental issue with the approach I followed to create instances using a separate variable file?</p>
  </li>
  <li><p>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?</p>
  </li>
  </ol>
  <p>I am <strong>not</strong> using Terraform Enterprise or Terraform Cloud. Please let me know how to proceed. Thanks!</p>
  <p>I use the below command to apply terraform changes</p>
  <pre><code>terraform apply -var-file=customers/apple.tfvars -state=statefile/apple.tfstate
  terraform apply -var-file=customers/walmart.tfvars -state=statefile/walmart.tfstate
  </code></pre>
  <p>Folder structure</p>
  <pre><code>  ├── 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
  
  
  </code></pre>