Terraform Best Practices

Chapter 1 of 6 · 4 practices · 3 min · 3-question quiz

Structure and style

How to lay out files, name things and format code so every Terraform change is easy to read in a pull request.

Start: Use the standard file layout for every root module

4 practices in this chapter

  1. 1Use the standard file layout for every root moduleWhen every configuration splits its blocks the same way, people find things without searching. The Terraform style guide recommends: As a configuration grows, split main.tf by logical group such as…
  2. 2Run fmt, validate and a linter before every commitFormatting arguments in a review is a waste of everyone's time. The style guide says to run terraform fmt and terraform validate before each commit, ideally from a Git pre-commit hook, and to use…
  3. 3Name resources with descriptive nouns, without repeating the typeA resource address already contains its type, so aws_instance.web_server_instance says "instance" twice. The style guide recommends descriptive nouns with underscores, and not repeating the…
  4. 4Give every variable and output a type and a descriptionUntyped variables accept anything, so a typo in a .tfvars file shows up as a confusing error deep in a resource, or not at all. The style guide says every variable needs a type and a…

Check yourself on this chapter

3 questions, right here once you are signed in. Your progress is saved as you read.

Sign up for the quiz