Terraform-Associate-004 practice question 35 of 35
Which command should you run to check if all code in a Terraform configuration that references multiple modules is properly formatted without making changes?
Full set: 360 questionsThe terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. To satisfy the requirement of checking formatting without applying changes, the -check flag is used. This flag makes the command exit with a non-zero status code if any files are not correctly formatted, making it ideal for CI/CD validation pipelines. Since the configuration references multiple modules, which are typically located in subdirectories, the -recursive flag is also required to ensure the command processes files in the entire module tree, not just the current directory.
A. terraform fmt -write-false: This prevents writing changes but does not provide a clear exit code for automation to indicate if files are unformatted, unlike the dedicated -check flag.
B. terraform fmt -list -recursive: This lists the files that need formatting but does not provide the explicit pass/fail exit code that the -check flag does for validation purposes.
D. terraform fmt -check: This option correctly checks the formatting but fails to process the files within the referenced modules, as it does not operate recursively on subdirectories.
1. HashiCorp Terraform Documentation, fmt Command:
Section: Options
-check: "Check if the input is formatted. Exit status will be 0 if all input is formatted correctly and 1 if any input is not formatted. An error message will be printed to stderr for each file that is not formatted."
-recursive: "Also process files in subdirectories. By default, only files in the given directory are processed."
Source: HashiCorp, Terraform CLI - Command: fmt, developer.hashicorp.com/terraform/cli/commands/fmt. Accessed May 2024.
Keep practicing
Take the free Terraform-Associate-004 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Terraform-Associate-004 practice questions
- Question 1You should run terraform fnt to rewrite all Terraform configurations within the current w…
- Question 2Module variable assignments are inherited from the parent module and you do not need to e…
- Question 3terraform validate confirms thesyntaxof Terraform files.
- Question 4Using the terraform state rm command against a resource will destroy it.
- Question 5Variables declared within a module are accessible outside of the module.
- Question 6terraform init retrieves and caches the configuration for all remote modules.
