Terraform best practices — how to use modules
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’.
- 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.
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.