Terraform best practices — how to structure your Terraform projects.

Jack Roper
CodeX
Published in
5 min readApr 8, 2021

--

When starting out with Terraform it’s hard to know what is considered ‘best practice’ in a number of areas.

This post is the first in the series which focuses on point 1 in the list, ‘how to structure your Terraform projects’.

  1. Use a consistent file structure across your projects.
  2. Use modules wherever possible.
  3. Use a consistent naming convention.
  4. Use a consistent format and style.
  5. Hold your state file remotely, not on your local machine.
  6. Avoid hardcoding variables.
  7. Fewer resources in a project are easier and faster to work with.
  8. Limit resources in the project to reduce the blast radius.
  9. Test your code.

Terraform project structure

The way you split your Terraform code into files is subjective. For a very simple project, it may absolutely be easier to lump everything together in one file and forget about it. However, as best practice, I like to structure my files for every project I create large or small in the same way, that way the project can easily be scaled without having to unpick it if you decide to restructure.

--

--

Jack Roper
CodeX
Writer for

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