Terraform best practices — how to use modules

Jack Roper
CodeX
Published in
6 min readApr 9, 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 second in the series which focuses on point 2 in the list, ‘use modules wherever possible’.

  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.

What are ‘Modules’ in Terraform?

A Module is simply a collection of .tf configuration files that define multiple related resources, coded in such a way that the code can be reused. These files are held in a folder.

If you use modules, you will have folders in your project structure. These modules (folders) can also be nested (sub-folders), although it is recommended not to go more than 3/4 levels deep and to avoid this if possible to reduce complexity.

--

--

Jack Roper
CodeX
Writer for

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