Terraform · Infrastructure as code

What does terraform init do?

Easyterraform initprovidersbackendUpdated Oct 2026
Quiz this track

Short answer

terraform init prepares a working directory: it downloads providers and modules, configures the backend and writes the dependency lock file. Re-run it after changing providers, modules or backend settings.

Terraform cannot plan until the working directory is initialized. Init reads the configuration, resolves provider and module sources, installs them into the .terraform directory, and connects to the configured backend. It also creates or updates .terraform.lock.hcl.

sh
terraform init
terraform init -upgrade
terraform init -reconfigure
terraform init -migrate-state
Choose the flag deliberately: upgrade changes versions, reconfigure ignores old backend settings, migrate-state copies state.

What the flags mean

  • -upgrade ignores the lock file and selects newer allowed provider versions
  • -reconfigure forgets the previous backend configuration
  • -migrate-state asks Terraform to copy state to the new backend
  • -backend=false skips backend setup for validation-only jobs

Init is safe to re-run, but it is not read-only when it migrates state or upgrades providers. In CI, cache the provider download directory and commit the lock file so every runner resolves the same builds.

Do not commit the .terraform directory. It contains downloaded binaries and local backend metadata. Commit the lock file instead.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between -upgrade and -reconfigure?
  2. 2Why should .terraform not be committed?
  3. 3What does the lock file record?
  4. 4How do you speed up init in CI?

Say it in the interview

terraform init prepares the working directory by installing providers and modules, configuring the backend and writing the lock file. Re-running it is normal after configuration changes. The flags matter: upgrade can change provider versions, reconfigure drops the old backend settings, and migrate-state moves state. I commit the lock file but not the .terraform directory.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.