0

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. I saw a similar question and started using terragrunt. Even with terragrunt I am not able to have a separate state file for each customer

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

My Intial Question

Module Repo structure ( When state file was local)

  ├── 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

With terragrunt, I separated my variable files and modules into two different repos

With terragrunt my Module Repo structure

  ├── main.tf
  └── variables.tf
  └── terragrunt.hcl
  ├── gcp_compute_disk
  │   ├── gcp_compute_disk.tf
  │   └── variables.tf
  ├── gcp_instance
  │   ├── gcp_instance.tf
  │   └── variables.tf

With terragrunt my variable Repo structure

.
├── apple-preprod
│   └── terragrunt.hcl
├── apple-prod
│   └── terragrunt.hcl
└── walmart-prod
|    └── terragrunt.hcl
├── walmart-preprod
│   └── terragrunt.hcl

Now how do I make a separate state file for each customer? is it possible to use terragrunt to achieve this scenario?

Eva
  • 515
  • 4
  • 28
  • 1
    You already asked that https://stackoverflow.com/questions/69202832/terraform-remote-statefile-for-multiple-instances-of-same-configuration-and-terr Can you clarify what is different this time then before? What's wrong with workspace as it was suggested or there possibilities given before? – Marcin Sep 23 '21 at 00:05
  • I understand developers start twitching when you duplicate code, but seriously, just make 3 copies of the Terraform code, one for each customer. Sure, each customer might start with a baseline, but there's no way Customer A is still going to have the same infrastructure as Customer C in 6 months. – Adam Vincent Sep 23 '21 at 02:55
  • @Marcin this question is regarding terragunt. I saw a post where someone had same issue and they suggested terragrunt. I didn't change the title, but questions is completely regarding terragrunt. And the problem I see with workspaces is, I have 40 + Customers and have 20 ppl in my team and the code is hosted on a jumpserver. If someone forgot to switch workspace, he could endup messing up other workspace state – Eva Sep 23 '21 at 06:44
  • @adam Vincent, the problem is I have 40+ customers and each customer has dev, preprod and prod – Eva Sep 23 '21 at 06:47
  • I stand by my solution. There is a point where your business grows beyond the capabilities of freeware tech and tooling. 40+ Customers is a good problem to have, meaning it might be time to start investing in TF Workspaces/Cloud/Enterprise or begin refactoring to improve your process so your customers can grow independently instead of trying to maintain one code base. – Adam Vincent Sep 27 '21 at 12:08

0 Answers0