Initializing a Working Directory
terraform init prepares a working directory for every other command. This lesson explains what it does, what it creates and the options that control it.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what terraform init does and the files it creates.
- Choose the init option for upgrading, reconfiguring or migrating.
What init does
A working directory is a directory of Terraform configuration files, the root module. terraform init performs the steps in the following sequence, printing a line for each.
-
Initialize the backend
Configures the local backend, the configured remote backend or the HCP Terraform cloud integration, and offers to migrate state if the backend changed.
-
Install modules
Downloads every remote child module into .terraform/modules; local path modules are used in place.
-
Install providers
Selects provider versions from the lock file or the constraints and installs them into .terraform/providers.
-
Write the lock file
Creates or updates .terraform.lock.hcl with versions and checksums.
init is safe to run repeatedly. It never changes infrastructure, never deletes configuration and never deletes state. Run it after cloning a repository, after adding or changing a provider requirement, after adding a module block or changing a module's source or version, and after changing the backend or cloud block. Other commands detect a missing or outdated initialization and tell you to run init.
The following diagram shows the error after a new module block, and what init changes.

Do I need to run init?
Files and directories
init creates two things in the root module directory. The following table explains each and whether it belongs in version control.
| Item | Contents | Version control |
|---|---|---|
| .terraform/ | Installed providers, downloaded modules and the saved backend settings | No: add to .gitignore |
| .terraform.lock.hcl | Selected provider versions and checksums | Yes: commit it |
The .terraform/terraform.tfstate file inside the directory stores the backend settings, not the infrastructure state. Deleting .terraform is harmless; init recreates it.
Options
The following options change how init works. Each is common in exam questions and in pipelines.
| Option | Effect |
|---|---|
| -upgrade | Ignore locked versions; install the newest provider and module versions the constraints allow |
| -reconfigure | Initialize the backend afresh, ignoring saved settings, without migrating state |
| -migrate-state | Copy existing state to the newly configured backend |
| -backend=false | Skip backend initialization, for example to run validate in CI |
| -backend-config=FILE or KEY=VALUE | Supply backend arguments for partial configuration |
| -lockfile=readonly | Fail instead of changing the lock file |
| -input=false | Never prompt; fail if input is needed |
Pipelines and upgrades
A pipeline that must use exactly the committed provider versions runs terraform init -input=false -lockfile=readonly. A developer who wants newer versions runs terraform init -upgrade and commits the updated lock file.
init options
After adding a module
An engineer adds a module "vpc" block that uses a registry module and runs terraform plan. Terraform reports "Module not installed". What should they do, and will it affect existing infrastructure?
The decisive fact is that modules are installed by init.
They run terraform init, which downloads the module into .terraform/modules and installs any new providers it needs, updating the lock file. init makes no changes to infrastructure or state, so it is safe. The next plan then includes the module's resources.
Which file created by terraform init should be committed to version control?
Show answer
The .terraform.lock.hcl dependency lock file. The .terraform directory should not be committed.
Exam Tips
Remember for the exam
- init: backend, modules, providers, lock file.
- init is safe to re-run and never changes infrastructure.
- Commit .terraform.lock.hcl; ignore .terraform/.
- -upgrade, -reconfigure, -migrate-state and -backend=false are the key options.