🎓 A1Learner
🔍
0 of 50 lessons completed

terraform init

Terraform init is the command that prepares a working folder. It downloads the providers the configuration names.

The command

Run it in the folder that holds the configuration. It needs no extra arguments for a first run.

terraform init

What .terraform holds

The folder holds the downloaded provider files, grouped by source and version.

.terraform/
  providers/
    registry.terraform.io/
      hashicorp/
        local/
          2.9.1/

Update a provider

The upgrade flag asks for the newest version allowed by the pin. Use it only when you mean to update.

terraform init -upgrade

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.9.1"
    }
  }
}

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = "Hello from Terraform."
}

# Command:
# terraform init

Key points

  • Init reads every provider block in the configuration.
  • It downloads each provider at the pinned version.
  • It creates a .terraform folder to hold the downloaded providers.
  • It also creates a lock file that records the exact provider versions.
  • Run init once per folder, and again after a provider is added or changed.
  • Init changes nothing in the cloud. It only prepares the folder.

Init reads the provider blocks and downloads the local provider. The working folder is then ready for plan and apply. The provider files stay inside the folder.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does terraform init do?