Terraform Best Practices — Using variables

Jack Roper
CodeX
Published in
6 min readApr 15, 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 sixth in the series which focuses on point 6 in the list, ‘Avoid hardcoding variables’, and also shows best practices for general variable use in Terraform.

  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.

Variables — Best Practice

Variables are typically defined in the variables.tf file in your Terraform project. Using variables allow you to modify aspects of the module without modifying the code in the module itself.

As our goal is to make our modules reusable, hardcoding variable values in those modules is a bad idea. Instead, those values should be passed into the module as required.

--

--

Jack Roper
CodeX
Writer for

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