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
What does terraform fmt do by default?
- Checks references and reports errors
- Sorts all resources alphabetically across files
- Rewrites files in the current directory to canonical format
- 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.
Which naming conventions match the Terraform style guidance?
- Use hyphens between words in resource names
- Separate words in names with underscores
- Use camelCase for resource names
- Do not repeat the resource type inside the name
- 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.
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.
