HashiCorp Terraform Associate (004) Question 57
Single answer3b Initialize a Terraform working directoryA platform engineer clones a Terraform configuration from a shared repository into a new local directory. The configuration already includes a required provider block and a remote backend configuration for storing state in Terraform Cloud. When the engineer immediately runs terraform plan, Terraform reports that the working directory has not been initialized. The engineer wants to prepare this new directory correctly without making infrastructure changes yet. Which action should the engineer take first?
- A
Run
terraform initin the working directory to install required providers and initialize the backend - B
Run
terraform apply -refresh-onlyso Terraform can initialize the directory while avoiding configuration changes - C
Run
terraform validateto download the required provider plugins and confirm the backend configuration - D
Run
terraform fmt -recursiveto prepare the configuration files before Terraform can initialize the directory
Show answer and explanation
Correct answer: A
Explanation
In Terraform, a newly created or newly cloned working directory must be initialized before commands like terraform plan or terraform apply can run successfully. The correct command is terraform init, which performs several setup tasks such as initializing backend settings, downloading and installing provider plugins, and preparing the .terraform working directory data. This is especially important when the configuration uses a remote backend, because Terraform must initialize backend access before it can read or manage state. According to Terraform CLI documentation and standard workflow best practices, the usual sequence is terraform init, then optional checks like terraform fmt and terraform validate, followed by terraform plan and terraform apply.
- A. Correct.
Correct.
terraform initis the required first step for a new or copied working directory. It initializes the working directory, installs the providers required by the configuration, and initializes the configured backend. This does not create or modify infrastructure by itself; it prepares Terraform to perform later operations such asplanandapply. - B. Incorrect.
Incorrect.
terraform apply -refresh-onlyis still an operation that requires the directory to already be initialized. It is used to update state and outputs based on real infrastructure without proposing configuration changes, but it does not replace initialization. - C. Incorrect.
Incorrect.
terraform validatechecks whether the configuration is syntactically valid and internally consistent, but it is not the command used to initialize a working directory. A common misconception is that validation will fetch everything Terraform needs, but initialization is a separate step handled byterraform init. - D. Incorrect.
Incorrect.
terraform fmt -recursiveformats Terraform configuration files for readability and consistency. It is useful for code quality and collaboration, but it does not install providers, configure the backend, or initialize the working directory.