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

Members can save this page, take notes and track progress. See membership · Sign in

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:

  1. Refresh: Terraform calls the AWS API to describe the instance. AWS returns "not found." Terraform updates its in-memory state to remove the instance.
  2. Diff: Terraform compares the refreshed state (instance missing) with the configuration (instance declared). The diff shows: aws_instance.web will be created.
  3. 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-only refreshes state without proposing changes — useful as a drift-detection cron job.

Prevention:

  • Restrict console/CLI write access via IAM policies.
  • Enable prevent_destroy lifecycle 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:TerminateInstances for 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-only or 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

← All Terraform questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake