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:
- Providers — reads
required_providersand the lock file, then downloads the right plugins into.terraform/providers/. - Modules — downloads any modules the configuration calls into
.terraform/modules/. - 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.
- Lock file — creates
.terraform.lock.hclif 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 secretsinit 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.
New clone
You have
.tffiles but no.terraform/folder. Runterraform 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.
What does terraform init never do?
Which of these should be committed to Git?
A teammate adds a new module call. What must you run before plan works again?
Where does init put downloaded provider plugins?
Remember
initinstalls 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.
