Hashicorp Certified Terraform Associate 003 · Free Practice Question Easy
Question 5
Your colleague provided you with a Terraform configuration file and you're having trouble reading it because parameters and blocks are not properly aligned. What command can you run to quickly update the file configuration file to make it easier to consume?
-
A
terraform init -
B
terraform state -
C
terraform workspace -
D
terraform fmt
Reveal correct answer
Correct answer: D
Explanation
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.
Other Terraform commands that generate Terraform configuration will produce configuration files that conform to the style imposed by terraform fmt, so using this style in your own files will ensure consistency.
The canonical format may change in minor ways between Terraform versions, so after upgrading Terraform we recommend to proactively run terraform fmt on your modules along with any other changes you are making to adopt the new version.
If you want to format ALL of your .tf files, you can use terraform fmt -recursive and it'll format all files in the current and all subdirectories.
WRONG ANSWERS:
terraform init - this is used to initialize and work with the Terraform backend
terraform state - this command is for working with and viewing Terraform state
terraform workspace - this command is used to create and manage Terraform Community workspaces
A. The terraform init command is used to initialize a Terraform working directory and download any necessary plugins. It is not related to formatting or aligning parameters and blocks in the configuration file.
B. The terraform state command is used to interact with Terraform state files, allowing you to inspect and modify the state of your infrastructure. It does not address the issue of aligning parameters and blocks in the configuration file.
C. The terraform workspace command is used to manage workspaces in Terraform, allowing you to switch between different sets of infrastructure resources. It does not help in formatting or aligning parameters and blocks in the configuration file.
D. The terraform fmt command is used to automatically format Terraform configuration files according to the Terraform style conventions. Running this command will align parameters and blocks properly, making the file easier to read and consume.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
