Lesson 2 of 4 Initializing, Validating and Formatting Configuration

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.

  1. 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.

  2. Install modules

    Downloads every remote child module into .terraform/modules; local path modules are used in place.

  3. Install providers

    Selects provider versions from the lock file or the constraints and installs them into .terraform/providers.

  4. 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.

Why plan reports “Module not installed” after a module block is added, and what init does about it. Init downloads the registry module into .terraform/modules, installs any new providers it needs and updates the lock file; it changes no infrastructure and no state. The next plan includes the module's resources.
The module name is illustrative.
Sort into groups

Do I need to run init?

Exam skills 3.2.1

Decide whether each change requires terraform init before the next plan.

Log in to try this activity Interactive activities are free with an account.

Files and directories

init creates two things in the root module directory. The following table explains each and whether it belongs in version control.

ItemContentsVersion control
.terraform/Installed providers, downloaded modules and the saved backend settingsNo: add to .gitignore
.terraform.lock.hclSelected provider versions and checksumsYes: 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.

OptionEffect
-upgradeIgnore locked versions; install the newest provider and module versions the constraints allow
-reconfigureInitialize the backend afresh, ignoring saved settings, without migrating state
-migrate-stateCopy existing state to the newly configured backend
-backend=falseSkip backend initialization, for example to run validate in CI
-backend-config=FILE or KEY=VALUESupply backend arguments for partial configuration
-lockfile=readonlyFail instead of changing the lock file
-input=falseNever 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.

Match the pairs

init options

Exam skills 3.2.2

Match each situation with the terraform init option it needs.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.