Terraform interview question · Most asked
Someone manually deletes an EC2 instance that Terraform manages. What happens on the next plan and apply?
Scenario Asked in DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE) interviews
Summary
Terraform refreshes state against real infrastructure and discovers the instance is gone. The plan shows the instance will be created again. Apply recreates it. The manual deletion is effectively undone — Terraform enforces the declared state.
Longer Answer
When you run terraform plan, Terraform performs a refresh as the first step: it
queries AWS for the current state of every resource in its state file. For the deleted
EC2 instance:
- Refresh: Terraform calls the AWS API to describe the instance. AWS returns "not found." Terraform updates its in-memory state to remove the instance.
- Diff: Terraform compares the refreshed state (instance missing) with the
configuration (instance declared). The diff shows:
aws_instance.web will be created. - Apply: Terraform creates a new instance with the same configuration. The new instance gets a new instance ID, a new private IP (unless you're using an Elastic IP or a fixed ENI), and new EBS volumes (data from the old volumes is lost unless they were separately managed or backed up).
What you lose:
- The instance ID changes — anything referencing the old ID (load balancer target registrations, DNS entries, monitoring) needs to update.
- Ephemeral storage (instance store volumes) and local data are gone.
- If the EBS root volume was set to
delete_on_termination = true(the default), the data is lost.
Detection without waiting for plan:
- AWS Config rules or CloudTrail can alert on manual changes in real time.
terraform plan -refresh-onlyrefreshes state without proposing changes — useful as a drift-detection cron job.
Prevention:
- Restrict console/CLI write access via IAM policies.
- Enable
prevent_destroylifecycle on critical resources so Terraform itself refuses to delete them (though this doesn't stop someone using the AWS console). - Use AWS resource-level permissions: deny
ec2:TerminateInstancesfor all but the Terraform role.
Key Points
- Terraform refresh detects the instance is gone; the plan proposes recreating it
- The new instance gets a new ID, IP and volumes — data is lost unless separately managed
- Drift detection:
terraform plan -refresh-onlyor AWS Config / CloudTrail alerts - Prevention: IAM policies that deny manual termination outside the Terraform role
Explanation
What interviewers look for
Look for understanding of the refresh-diff-apply cycle and what changes when an instance is recreated (new ID, new IP, lost data). Weak answers say "Terraform recreates it" without explaining what's lost. Strong answers mention detection and prevention mechanisms.
More Terraform questions
- When do you need depends_on in Terraform, and when is it a code smell?
- What is Terraform, and how does it work at a high level?
- How do you configure OIDC authentication between GitHub Actions and AWS for Terraform pipelines?
- How do you use Terraform to deploy the same infrastructure to nonprod and production AWS accounts?
- What are the main terraform state subcommands, and when do you use them?
- Terraform apply fails halfway through — some resources were created, others were not. How do you recover?
- How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
- Walk through terraform init, plan and apply. What does each do?