Bird
Raised Fist0
Terraformcloud~20 mins

Terraform fmt for formatting - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Terraform Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What is the effect of running terraform fmt on a Terraform project?
You have a Terraform project with inconsistent indentation and spacing. What does running terraform fmt do to the project files?
AIt validates the Terraform files for syntax errors but does not change any formatting.
BIt reformats all Terraform files to use consistent indentation and spacing according to Terraform style conventions.
CIt initializes the Terraform backend and downloads required providers.
DIt applies the Terraform configuration to create or update infrastructure.
Attempts:
2 left
💡 Hint
Think about a tool that cleans up code style without changing its meaning.
❓ Configuration
intermediate
2:00remaining
Which command formats Terraform files recursively in all subdirectories?
You want to format all Terraform files in the current directory and all nested folders. Which terraform fmt command option achieves this?
Aterraform fmt -r
Bterraform fmt -all
Cterraform fmt --deep
Dterraform fmt -recursive
Attempts:
2 left
💡 Hint
Look for the official flag that means 'go into all folders'.
🧠 Conceptual
advanced
2:00remaining
What happens if terraform fmt is run on a file with syntax errors?
Consider a Terraform file with a missing closing brace. What will terraform fmt do when run on this file?
AIt will report an error and refuse to format the file.
BIt will delete the file to prevent errors.
CIt will format the file ignoring syntax errors and produce a formatted file anyway.
DIt will fix the syntax error automatically and format the file.
Attempts:
2 left
💡 Hint
Think about whether formatting tools fix code logic or just style.
✅ Best Practice
advanced
2:00remaining
Why is it recommended to run terraform fmt before committing code to version control?
What is the main benefit of running terraform fmt on your Terraform files before pushing changes to a shared repository?
AIt ensures consistent code style, reducing unnecessary diffs and making reviews easier.
BIt encrypts the Terraform files to protect sensitive data.
CIt automatically applies the infrastructure changes to the cloud.
DIt compresses the files to save storage space in the repository.
Attempts:
2 left
💡 Hint
Think about how consistent formatting helps teams work together.
❓ Architecture
expert
2:00remaining
In a CI/CD pipeline, where should terraform fmt be integrated for best practice?
You are designing a CI/CD pipeline for Terraform infrastructure code. At which stage should terraform fmt be run to ensure code quality and prevent style issues from merging?
AAs a manual step after deployment to check formatting.
BAs a pre-commit hook on developers' machines before pushing code.
CAs an automated check in the pipeline before merging pull requests.
DOnly after applying infrastructure changes to production.
Attempts:
2 left
💡 Hint
Consider automation that prevents bad code from entering the main branch.

Practice

(1/5)
1. What is the main purpose of the terraform fmt command?
easy
A. To check for syntax errors in Terraform files
B. To deploy infrastructure to the cloud
C. To automatically format Terraform configuration files for readability
D. To destroy all Terraform-managed resources

Solution

  1. Step 1: Understand the role of terraform fmt

    This command is designed to clean up and standardize the formatting of Terraform files, making them easier to read and maintain.
  2. Step 2: Differentiate from other commands

    Unlike commands that deploy or destroy resources, terraform fmt only changes the file layout without affecting infrastructure.
  3. Final Answer:

    To automatically format Terraform configuration files for readability -> Option C
  4. Quick Check:

    Formatting = Readability [OK]
Hint: Remember: fmt = format code, not deploy or destroy [OK]
Common Mistakes:
  • Confusing fmt with terraform apply
  • Thinking fmt checks syntax errors
  • Assuming fmt manages resources
2. Which of the following is the correct way to run terraform fmt on all files in the current directory?
easy
A. terraform fmt
B. terraform fmt -recursive
C. terraform fmt -all
D. terraform fmt --format-all

Solution

  1. Step 1: Recall default behavior of terraform fmt

    By default, running terraform fmt without flags formats all Terraform files in the current directory.
  2. Step 2: Check other options

    Options like -recursive exist to format subdirectories, but -all and --format-all are invalid flags.
  3. Final Answer:

    terraform fmt -> Option A
  4. Quick Check:

    Default fmt formats current directory [OK]
Hint: No flags needed to format current folder files [OK]
Common Mistakes:
  • Using invalid flags like -all or --format-all
  • Confusing recursive flag with default behavior
  • Expecting fmt to format files outside current directory by default
3. Given this Terraform snippet before formatting:
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type= "t2.micro"
}

What will terraform fmt do to this code?
medium
A. Add comments explaining each line
B. Add spaces around the equals sign and indent the block properly
C. Remove the resource block entirely
D. Change the resource type to "aws_server"

Solution

  1. Step 1: Identify formatting issues

    The snippet lacks spaces around the equals sign and proper indentation inside the resource block.
  2. Step 2: Understand what terraform fmt fixes

    It adds spaces around operators and indents blocks correctly but does not change resource types or add comments.
  3. Final Answer:

    Add spaces around the equals sign and indent the block properly -> Option B
  4. Quick Check:

    Fmt fixes spacing and indentation [OK]
Hint: Fmt fixes spacing and indentation, not content [OK]
Common Mistakes:
  • Thinking fmt changes resource types
  • Expecting fmt to add comments
  • Assuming fmt deletes code blocks
4. You ran terraform fmt but your Terraform files still look messy. What is the most likely reason?
medium
A. Terraform fmt does not change file formatting
B. Terraform fmt only works on .tfvars files
C. Your files have syntax errors preventing formatting
D. You ran terraform fmt in the wrong directory

Solution

  1. Step 1: Check where terraform fmt runs

    The command formats files in the current directory by default, so running it elsewhere won't affect your target files.
  2. Step 2: Eliminate other options

    Terraform fmt does change formatting, works on .tf files, and does not require syntax correctness to format.
  3. Final Answer:

    You ran terraform fmt in the wrong directory -> Option D
  4. Quick Check:

    Run fmt where files are located [OK]
Hint: Run fmt in folder with your Terraform files [OK]
Common Mistakes:
  • Assuming fmt fixes syntax errors
  • Thinking fmt works on .tfvars files only
  • Expecting fmt to format files outside current folder
5. You want to ensure all Terraform files in your project and all subfolders are formatted consistently before committing code. Which command should you use?
hard
A. terraform fmt -recursive
B. terraform fmt -all
C. terraform fmt --check
D. terraform fmt --init

Solution

  1. Step 1: Understand recursive formatting

    The -recursive flag tells terraform fmt to format files in the current directory and all subdirectories.
  2. Step 2: Review other options

    -all and --init are invalid flags; --check only verifies formatting without changing files.
  3. Final Answer:

    terraform fmt -recursive -> Option A
  4. Quick Check:

    Recursive flag formats all folders [OK]
Hint: Use -recursive to format all subfolders [OK]
Common Mistakes:
  • Using invalid flags like -all
  • Confusing --check with formatting action
  • Trying to use --init for formatting