TF-004 Read, generate and modify configuration Practice Question
An operator modifies a Terraform configuration to change the `ami` attribute of an `aws_instance` resource. When they run `terraform plan`, they see that the resource will be destroyed and recreated. They want to avoid the recreation and instead update the instance in-place. What is the best approach?
⚠ Common exam trap
The exam tests the misconception that lifecycle rules like `create_before_destroy` or `ignore_changes` can circumvent provider-level force-new attributes, when in fact they only affect Terraform's behavior, not the underlying API constraints.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Accept the recreation; it is required for this attribute
Changing the `ami` attribute of an `aws_instance` resource is a force-new attribute in the AWS provider. Terraform cannot update the AMI of a running EC2 instance in-place; it must destroy the existing instance and create a new one with the new AMI. This is a fundamental constraint of the AWS API, not a Terraform limitation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Add `create_before_destroy` lifecycle rule
Why it's wrong here
The `create_before_destroy` lifecycle rule instructs Terraform to create a new resource instance *before* destroying the old one when an in-place update is not possible. While this minimizes downtime for services by ensuring a replacement is ready, it does not prevent the underlying recreation event itself. The original resource will still be destroyed, and a new one will be created, which is precisely what the operator is trying to avoid if the goal is to prevent *any* recreation.
- ✓
Accept the recreation; it is required for this attribute
Why this is correct
Changing the Amazon Machine Image (AMI) ID for an existing AWS EC2 instance resource typically necessitates its recreation. The AMI is a fundamental, immutable property of an EC2 instance that cannot be modified in-place after the instance has been launched. Therefore, to apply an updated AMI, Terraform must destroy the old instance and provision a completely new one with the specified new AMI.
- ✗
Use `ignore_changes` in lifecycle to ignore AMI changes
Why it's wrong here
Applying `ignore_changes` to the `ami` attribute within the `lifecycle` block would instruct Terraform to disregard any future modifications to that specific attribute in the configuration. While this would prevent Terraform from planning a recreation, it would also mean the desired AMI update would never be applied to the resource. The EC2 instance would continue to use its original AMI, effectively ignoring the operator's intended change.
- ✗
Add `prevent_destroy` lifecycle rule
Why it's wrong here
The `prevent_destroy = true` lifecycle rule is designed to safeguard critical resources by explicitly blocking any Terraform plan that includes a destroy action for that resource. If a change, such as an AMI update, requires recreation (destroying the old instance), this rule would cause the `terraform plan` or `terraform apply` command to fail. It prevents *any* destroy, thereby blocking the necessary recreation and preventing the AMI change from being applied.
Go deeper
Related to this question
About these practice questions
Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.