Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Providers and Project Initialization  ›  Capstone Project

Initialising a Project with terraform init

Project Initialization 12 minterraform init prepares a working folder by installing the required providers and modules into .terraform, setting up the state backend and writing the lock file, and it is safe to run again whenever those inputs change.

Initialising a Project with terraform init

You clone Crumb's repository, type terraform plan, and get an error telling you to run terraform init. The .tf files are there, but the tools they need are not. init is the step that fetches them.

The rule

init fetches what the files ask for; run it first and whenever those needs change.

terraform init reads the configuration and prepares the folder:

  1. Providers — reads required_providers and the lock file, then downloads the right plugins into .terraform/providers/.
  2. Modules — downloads any modules the configuration calls into .terraform/modules/.
  3. Backend — sets up where state is stored: a local file by default, or a remote backend such as an S3 bucket if one is configured.
  4. Lock file — creates .terraform.lock.hcl if it is missing, or checks against it if present.
$ terraform init

Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.61.0...

Terraform has been successfully initialized!

What goes in Git afterwards:

.terraform/            # ignore: a local download cache
.terraform.lock.hcl    # commit: pins provider versions for everyone
terraform.tfstate      # ignore: holds real IDs and possibly secrets

init is safe to repeat. It never creates or destroys infrastructure. Useful flags: -upgrade to move providers to the newest allowed versions, and -reconfigure or -migrate-state when you change the backend.

When plan complains that a provider or module is missing, or does not match the lock file, the fix is almost always to run terraform init again. In pipelines, terraform init -input=false makes sure it never sits waiting for an answer.

The shortcut

Remember "new clone, new need, new backend". Any of the three means run init.

Crumb's first day in a new repositoryStep 1 of 4
  1. New clone

    You have .tf files but no .terraform/ folder. Run terraform init.

The trap: committing .terraform/. It holds large platform-specific binaries that do not work on other machines and bloat the repository.

Drills

Answer fast — trust the rule.

quick drill

What does terraform init never do?

quick drill

Which of these should be committed to Git?

quick drill

A teammate adds a new module call. What must you run before plan works again?

quick drill

Where does init put downloaded provider plugins?

Remember

  • init installs providers and modules, sets up the backend, and writes or checks the lock file.
  • Shortcut: new clone, new need, new backend: run init. It is always safe.
  • Trap: commit the lock file, never .terraform/ or local state.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA fresh clone

A new teammate clones Crumb's repository and runs terraform plan straight away, which fails. Explain why, what terraform init does, which of the files it creates belong in Git, and when they will need to run it again.