Interview Infrastructure as Code (Terraform, Ansible)

What does terraform init do?

Infrastructure as Code (Terraform, Ansible) · Basic level

Answer

terraform init prepares a working directory. It configures the backend, downloads required providers, installs child modules, creates or updates dependency lock information, and makes the directory ready for plan and apply. It is safe to run repeatedly and is normally the first command in local and CI workflows.

Technical explanation

Provider plugins are resolved from required_providers and recorded in .terraform.lock.hcl.

Backend initialization decides where state will be stored and may ask to migrate existing state.

Module installation downloads local, registry, Git, or archive-based child modules.

Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.

Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.

Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.

Hands-on example

1. Create a directory with main.tf, versions.tf, and a child module reference.

2. Run terraform init and inspect what changed:

terraform init

find .terraform -maxdepth 3 -type f | sort

cat .terraform.lock.hcl

3. Change only a provider version constraint and rerun terraform init -upgrade in a test branch to see provider selection update.

4. In CI, run init before validate/plan and fail the build if .terraform.lock.hcl changed but was not committed.

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Infrastructure as Code (Terraform, Ansible) interview questions

← All Infrastructure as Code (Terraform, Ansible) questions