0
votes

I am using Terraform to deploy an 3-tiers application on Azure: Frontend, Backend, and DB. It works well. The application is a product, and I need several instances for several customers.

My problem is, I have a hard time understanding applying several sets of values (variables.tf) to the same script, in order to get several environments.

I would like to have the following structure:

  • main.tf
  • customer1
    • variables.tf
  • customer2
    • variables.tf

And select whether I deploy customer1 or customer2. I read about terraform workspaces, and started creating one workspace per customer. But I don't understand how to apply a different set of values for the same scripts depending on the working workspace. It's difficult to find a comprehensive example online, what I am trying to do should be the bread and butter of terraform.

Thanks!

1
The linked answer is mostly explaining about when workspaces are valuable (dynamic environments where you can't/don't know ahead of time what will separate them) but also links to stackoverflow.com/questions/43201497/… which explains how to lay your code out for multi-environment/multi-tenancy without using workspaces.ydaetskcoR
Thanks, I will have a close look. I must say the official dock lacks concrete examples...Joel

1 Answers

0
votes

As bot you and @ydaetskcoR knew, workspace is one choice. If it is not suitable for your business. There is another way for you, and I would recommend it for your use case.

Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules.

So in your case, it can be easily used to manage different customers as below structure.

└── customer1
    ├── prod
    │   ├── app
    │   │   └── terraform.tfvars
    │   ├── mysql
    │   │   └── terraform.tfvars
    │   └── vpc
    │       └── terraform.tfvars
    └── stage
        ├── app
        │   └── terraform.tfvars
        ├── mysql
        │   └── terraform.tfvars
        └── vpc
            └── terraform.tfvars
└── customer2
    ├── prod
    │   ├── app
    │   │   └── terraform.tfvars
    │   ├── mysql
    │   │   └── terraform.tfvars
    │   └── vpc
    │       └── terraform.tfvars
    └── stage
        ├── app
        │   └── terraform.tfvars
        ├── mysql
        │   └── terraform.tfvars
        └── vpc
            └── terraform.tfvars