Bitzu

Terraform Associate 004 ยท Core Terraform workflow

terraform fmt and style

Canonical formatting, -check, -recursive, -diff, style conventions such as snake_case names and ordering (3g).

Key points

  • fmt rewrites configuration files to the canonical format and style and has no customization options.
  • By default it touches only the current directory; -recursive includes subdirectories, -check only reports (non-zero exit if something is unformatted), -diff shows the changes.
  • Style conventions: lowercase names with underscores, no resource type repeated in the name, two-space indentation, aligned equals signs.

Source: https://developer.hashicorp.com/terraform/cli/commands/fmt

Practice questions

Single choice

What does terraform fmt do by default?

  1. Checks references and reports errors
  2. Sorts all resources alphabetically across files
  3. Rewrites files in the current directory to canonical format
  4. Deletes unused variables
Show answer

Answer: C. Rewrites files in the current directory to canonical format

fmt rewrites configuration files to the canonical style. It does not check references (validate) or remove anything.

Select all that apply

Which naming conventions match the Terraform style guidance?

  1. Use hyphens between words in resource names
  2. Separate words in names with underscores
  3. Use camelCase for resource names
  4. Do not repeat the resource type inside the name
  5. Always prefix the name with the resource type
Show answer

Answer: B. Separate words in names with underscores, D. Do not repeat the resource type inside the name

Names are descriptive nouns with underscores, and the type is not repeated since the address already contains it. Hyphens, camelCase and type prefixes go against the guidance.

True or false

terraform fmt fails when a resource refers to another resource that does not exist.

Show answer

Answer: False

False. fmt only adjusts layout and style; reference errors are found by validate or plan.

Practice 8 questions on this topic in Bitzu

Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module