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
AWS: When would you pick S3 over EBS?
The right answer shows you understand *access patterns*, not just storage products.
interview
Explain the CAP Theorem in one paragraph
A distributed-systems classic that trips senior engineers as often as juniors.
interview
Difference between Process and Thread
An OS fundamentals question every backend engineer must nail.
Discussion (0)
No comments yet. Be the first to weigh in.