Terraform best practices — how to structure your Terraform projects.
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’.
- Use a consistent file structure across your projects.
- Use modules wherever possible.
- Use a consistent naming convention.
- Use a consistent format and style.
- Hold your state file remotely, not on your local machine.
- Avoid hardcoding variables.
- Fewer resources in a project are easier and faster to work with.
- Limit resources in the project to reduce the blast radius.
- 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.