FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

The BEST way to structure your Terraform projects!

Dynamic Terraform deployments across multiple environments

Jack Roper
FAUN — Developer Community 🐾
6 min readDec 22, 2021

--

The structure of your Terraform code is subjective! But here's my take on the ‘best’ way to lay it all out in my experience!

Photo by Blake Connally on Unsplash

The ideal structure needs to take into account the following principles:

  1. You should have a centralized module repository that you can reuse across multiple projects.
  2. You should use tags to version your modules that are held in your centralized repository.
  3. You should use a consistent file and folder structure across your project.
  4. You should be storing your pipelines as code, these can be contained within the project structure too.
  5. You should be ‘limiting the blast radius’, e.g. the code is contained to deploy certain parts of your solution, and the pipeline to run or destroy this can be run independently. e.g. You would separate the creation of your SQL servers from the creation of your App Gateways.
  6. You can make use of ‘replace tokens’ to enable deployments to multiple environments, e.g. DEV, UAT, PROD. They also help when defining variables that are common across many parts of your code, e.g. the Terraform version, Backend storage account, Backend Storage Resource group.

More on how to pull this off as we go through the article!

Use a centralized module repository

Modules can be consumed in multiple ways, most commonly the module code is local to the project you are working on, e.g. stored in a folder calledmodules. This could be a new module you have coded just for that project, or code copied from elsewhere.

This is fine until you want to update the module and reuse it for another project. The versions of the module code will become different across your projects unless you update them all together. The code calling the module will also need to be updated and tested. This is clearly not scalable!

Referencing public modules on the Terraform registry is useful in certain situations, however, these can prove inflexible as you don’t have full control over them. Therefore I would recommend…

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Jack Roper

A blog about DevOps & Cloud Tech. Specializing in Terraform, Kubernetes, GitHub, Azure & Azure DevOps! ☁️

Responses (10)

Write a response