Terraform Init
The terraform init command prepares a working directory by downloading providers, installing modules, and configuring the backend. Run it before plan or apply in a new directory and after changing providers, modules, or the backend.
What Is Terraform Init?
It is like npm install for Terraform. Providers and modules go into the local .terraform directory, and exact provider versions are recorded in .terraform.lock.hcl.
Bash
terraform init # providers, modules, backendterraform init -upgrade # newest versions allowed by constraintsterraform init -backend=false # skip the backend, handy for validate in CIRememberCommit
.terraform.lock.hcland add.terraform/to.gitignore.
Frequently Asked Questions
When do I need to run init again?
After adding or changing a provider, module, or backend, and after cloning the repo on a new machine or CI runner.
What goes wrong if the lock file is not committed?
Different runs can resolve different provider patch versions, so the same code can plan differently in different places.
Found a mistake, an outdated step or a broken link?