0 votes
1 view
in DevOps and Agile by (30k points)

We are utilizing Terraform heavily for AWS Cloud provisioning. Our base terraform structure looks like this:

├─ modules

├── x

├── y

├─ environments

├── dev

│   ├── main.tf

│   ├── output.tf

│   └── variables.tf

└── uat

│   ├── main.tf

│   ├── output.tf

│   └── variables.tf

└── prod

    ├── main.tf

    ├── output.tf

    └── variables.tf

As we reached a point where we have many modules and many environments, code duplication becomes a more serious headache now, we would like to get rid of as much of it as possible.

Our main concern currently is with the output.tf files - every time we extend an existing module or add a new module, we need to set up the environment specific configuration for it (this is expected), but we still have to copy/paste the required parts into output.tf to output the results of the provisioning (like IP addresses, AWS ARNs, etc.).

Is there a way to get rid of the duplicated output.tf files? Could we just define the wanted outputs in the modules themselves and see all defined outputs whenever we run terraform for a specific environment?

1 Answer

0 votes
by (51.2k points)

If your env prod and uat environments are having the same shape but with different properties then you could leverage workspaces to separate your environment state, along with separate *.tfvars files to specify the different configurations.

Let’s see a sample of how it looks like 

├─ modules

│   ├── x

│   └── y

├── dev.tfvars

├── prod.tfvars

├── uat.tfvars

├── main.tf

├── outputs.tf

└── variables.tf

With this, you can create a new workspace 

terraform workspace new uat

After deploying changes then it becomes 

terraform workspace select uat

terraform apply --var-file=uat.tfvars

Then the workspace feature ensures that different environments states are managed separately 
This approach only works when the differences between the environments are small enough that it makes sense to encapsulate the logic for that in the individual modules.
Welcome to Intellipaat Community. Get your technical queries answered by top developers !