Terraform · Infrastructure as code
What is Terraform and how does it work?
Short answer
Terraform is a declarative infrastructure-as-code tool. You describe the desired end state in HCL, and Terraform builds a dependency graph, compares it with state, and applies a plan to reach that state.
Terraform reads HCL configuration, resolves providers and variables, then builds a graph of resources and their dependencies. It compares that desired graph with the recorded state and the real APIs, produces a plan, and applies the changes in dependency order.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.60"
}
}
}
resource "aws_s3_bucket" "logs" {
bucket = "acme-logs-prod"
}The core workflow
- terraform init downloads providers and modules and configures the backend
- terraform plan refreshes state and shows the proposed changes
- terraform apply executes the plan and updates state
- terraform destroy reverses the managed resources
Terraform is not a configuration-management tool like Ansible. It provisions infrastructure through provider APIs and does not keep an agent on the machine. Bootstrapping software inside an instance is usually better handled by images, cloud-init or a dedicated configuration tool.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between Terraform and Ansible?
- 2What happens during terraform plan?
- 3Why does Terraform need state?
- 4How does Terraform order resource creation?
Say it in the interview
Terraform is a declarative infrastructure-as-code tool. I write the desired state in HCL, and it builds a dependency graph, compares that with state and the provider APIs, then applies a plan. The plan is where I look for replacements and destroys before anything changes. It provisions infrastructure, so I use other tools for in-instance configuration.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.