How does Terraform know what already exists?
The most missed Terraform interview follow-up. Explain state files and how they work.
Question
How does Terraform know what infrastructure already exists in the cloud?
Answer
Terraform keeps a **state file** (`terraform.tfstate`) that maps every resource in your `.tf` files to its real-world identifier (an AWS ARN, an Azure resource ID, etc.). On `terraform plan`, Terraform reads the state, calls the cloud APIs to refresh each resource's current attributes, and diffs them against your configuration to compute the plan. In a team setting you **must not keep state on your laptop** — you configure a **remote backend** (S3 + DynamoDB lock for AWS, Azure Storage for Azure, GCS for GCP) so everyone reads and writes the same state, and DynamoDB/blob leases prevent two people applying at the same time. If you lose the state file, Terraform will try to create the resources again — potentially causing duplicates or conflicts.
See the question and answer above.
Keep reading
You may also like
interview
Explain the Kafka message flow end-to-end
A crisp walkthrough of what happens between a producer's `send()` call and a consumer's `poll()` receiving the message.
interview
Difference between Docker and Kubernetes
One of the most common early-career interview questions. Nail the answer in 3 sentences.
interview
What happens when you type a URL in the browser?
The definitive end-to-end walkthrough — DNS to render.
Discussion (0)
No comments yet. Be the first to weigh in.