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
Recall & Review
beginner
What is the purpose of the terraform fmt command?
The terraform fmt command formats Terraform configuration files to a standard style, making them easier to read and maintain.
Click to reveal answer
beginner
Which files does terraform fmt format by default?
By default, terraform fmt formats all .tf files in the current directory.
Click to reveal answer
intermediate
How can you check what changes terraform fmt would make without applying them?
Use the -check option with terraform fmt to see if files need formatting without changing them.
Click to reveal answer
intermediate
What does the -recursive flag do in terraform fmt?
The -recursive flag tells terraform fmt to format files in the current directory and all subdirectories recursively.
Click to reveal answer
beginner
Why is consistent formatting important in Terraform projects?
Consistent formatting helps teams read and understand code easily, reduces errors, and improves collaboration.
Click to reveal answer
What does terraform fmt do?
ADeploys infrastructure
BValidates Terraform configuration
CFormats Terraform files to a standard style
DDestroys Terraform-managed infrastructure
✗ Incorrect
terraform fmt formats Terraform files to improve readability.
Which file extension does terraform fmt target by default?
A.tf
B.json
C.txt
D.yaml
✗ Incorrect
terraform fmt formats files ending with .tf.
How do you make terraform fmt check formatting without changing files?
A-write=false
B-check
C-validate
D-plan
✗ Incorrect
The -check flag checks formatting without modifying files.
What does the -recursive flag do?
AFormats files in parent directory
BFormats files in the current directory only
CFormats only the current file
DFormats files in current and all subdirectories
✗ Incorrect
The -recursive flag formats files in all subdirectories.
Why should teams use terraform fmt regularly?
ATo keep code consistent and readable
BTo increase file size
CTo deploy faster
DTo encrypt files
✗ Incorrect
Regular formatting keeps code consistent and easier to understand.
Explain what terraform fmt does and why it is useful.
Think about how clean and consistent code helps when working with others.
You got /4 concepts.
Describe how to use terraform fmt to check formatting in all subdirectories without changing files.
Combine two flags to check all files deeply without applying changes.
You got /4 concepts.
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
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.
Step 2: Differentiate from other commands
Unlike commands that deploy or destroy resources, terraform fmt only changes the file layout without affecting infrastructure.
Final Answer:
To automatically format Terraform configuration files for readability -> Option C
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
Step 1: Recall default behavior of terraform fmt
By default, running terraform fmt without flags formats all Terraform files in the current directory.
Step 2: Check other options
Options like -recursive exist to format subdirectories, but -all and --format-all are invalid flags.
Final Answer:
terraform fmt -> Option A
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
Step 1: Identify formatting issues
The snippet lacks spaces around the equals sign and proper indentation inside the resource block.
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.
Final Answer:
Add spaces around the equals sign and indent the block properly -> Option B
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
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.
Step 2: Eliminate other options
Terraform fmt does change formatting, works on .tf files, and does not require syntax correctness to format.
Final Answer:
You ran terraform fmt in the wrong directory -> Option D
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
Step 1: Understand recursive formatting
The -recursive flag tells terraform fmt to format files in the current directory and all subdirectories.
Step 2: Review other options
-all and --init are invalid flags; --check only verifies formatting without changing files.
Final Answer:
terraform fmt -recursive -> Option A
Quick Check:
Recursive flag formats all folders [OK]
Hint: Use -recursive to format all subfolders [OK]