Courseiva
Understand IaC conceptshardMultiple ChoiceObjective-mapped

TF-004 lifecycle Practice Question

Exhibit

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "WebServer"
  }
  lifecycle {
    create_before_destroy = true
  }
}

After applying the configuration above, the user changes the AMI to a new value and runs 'terraform apply'. Assuming the new AMI triggers a recreate, what happens during the apply?

⚠ Common exam trap

A common trap is assuming that Terraform defaults to create-before-destroy to minimize downtime. In reality, the default is destroy-before-create; you must explicitly set `create_before_destroy = true` to change this behavior.

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

The existing instance is deleted before the new one is created.

Terraform's default lifecycle for resource replacement is to destroy the existing resource first, then create the new one. This is the standard behavior unless `create_before_destroy` is explicitly set to `true`. The AMI change triggers a recreate, and by default, the old instance is deleted before the new one is provisioned.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The existing instance is deleted before the new one is created.

    Why this is correct

    This statement is correct. When an immutable attribute, such as the Amazon Machine Image (AMI) for an EC2 instance, is modified in the Terraform configuration, Terraform's default behavior is to destroy the existing resource before creating its replacement. This 'destroy-then-create' sequence is the standard approach for resource recreation unless explicitly overridden by `lifecycle` settings. This ensures resource consistency but typically results in a period of downtime for the affected service.

  • Both instances run simultaneously only if the instance type allows it.

    Why it's wrong here

    This statement is incorrect because Terraform's execution order for resource updates is governed by its `lifecycle` meta-arguments, specifically `create_before_destroy`, not by the inherent capabilities of the instance type itself. Whether an instance type 'allows' simultaneous operation is irrelevant to how Terraform orchestrates the replacement of a resource. By default, Terraform does not create a new instance concurrently with the old one, making this option misleading regarding Terraform's control over resource lifecycle.

  • The new instance is created first, then the old one is deleted.

    Why it's wrong here

    This statement is incorrect because the described behavior, where the new instance is created prior to the old one being deleted, only occurs when the `create_before_destroy = true` meta-argument is explicitly set within the resource's `lifecycle` block. Without this specific configuration, Terraform adheres to its default destroy-then-create strategy. Therefore, this option does not reflect Terraform's standard operational flow for resource replacement when an AMI is changed.

  • The plan fails because AMI change with create_before_destroy is not allowed.

    Why it's wrong here

    This statement is incorrect. Terraform is designed to manage changes to resource attributes, including immutable ones like an Amazon Machine Image (AMI), by planning a replacement operation. A change to an AMI will trigger a 'replace' action, not a plan failure, regardless of whether `create_before_destroy` is configured. The `lifecycle` block modifies the *order* of operations during replacement, it does not prevent valid attribute changes from being planned.

About these practice questions

This TF-004 question is part of Courseiva's 428-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.