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.
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
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?
- How does Terraform use DynamoDB for state locking with an S3 backend?