Module 3: Core Terraform Workflow

Lesson 3.2: Initializing a Working Directory

Plain language

terraform init prepares a directory so Terraform can work in it. It doesn't create any infrastructure.

Under the hood

init performs up to four tasks:

  1. Backend initialization. Configures where state is stored (local by default; remote in Module 6).
  2. Module installation. Downloads child modules into .terraform/modules/.
  3. Provider installation. Downloads providers into .terraform/providers/, honoring the lock file (Module 2).
  4. Lock file. Creates or updates .terraform.lock.hcl.

init is safe to re-run. It never touches infrastructure or modifies resources in state.

Re-run init when you:

  • Clone a repo or start in a new directory
  • Add or change a provider requirement
  • Add a module, or change a module's source or version
  • Add or change a backend or cloud block

You don't need to re-run it after changing resource arguments, variables, or outputs.

Flag Purpose
-upgrade Re-select the newest allowed provider and module versions
-backend=false Skip backend initialization (for example, to validate in CI without credentials)
-backend-config=... Supply partial backend settings (Module 6)
-migrate-state / -reconfigure Move state to a new backend, or ignore the existing backend configuration (Module 6)
-input=false Fail rather than prompt. Use this in automation.