Courseiva
Understand Terraform basicseasyMultiple ChoiceObjective-mapped

TF-004 Understand Terraform basics Practice Question

Exhibit

Refer to the exhibit.
```hcl
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "WebServer"
  }
}

resource "aws_eip" "ip" {
  instance = aws_instance.web.id
}
```

Refer to the exhibit. A developer runs `terraform apply` and the operation succeeds. Later, they manually terminate the EC2 instance through the AWS console. What will happen when the developer runs `terraform apply` again?

⚠ Common exam trap

Watch out — candidates often assume Terraform will fail or skip the EIP reassociation because the instance is terminated, but Terraform's state-driven reconciliation ensures it recreates and reassociates all resources to match the configuration, regardless of manual changes.

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

Terraform will recreate the EC2 instance and reassociate the Elastic IP

Terraform maintains the Elastic IP (EIP) association in its state file. When the EC2 instance is manually terminated outside of Terraform, the state still records the EIP as associated with that instance ID. On the next `terraform apply`, Terraform detects that the instance is missing (drift) and plans to recreate it, then reassociates the EIP to the new instance as defined in the configuration, ensuring the public IP remains attached.

Answer analysis

Option-by-option breakdown

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

  • Terraform will recreate the EC2 instance and reassociate the Elastic IP

    Why this is correct

    When an EC2 instance managed by Terraform is manually terminated, Terraform detects this as drift during the `plan` phase. The `aws_instance` resource will be marked for recreation. Since the `aws_eip_association` resource explicitly depends on the `id` of the EC2 instance, Terraform will also identify that the existing Elastic IP needs to be reassociated with the *new* instance. Consequently, Terraform will first provision a new EC2 instance and then update the `aws_eip_association` to link the existing Elastic IP to this newly created instance, restoring the desired state.

  • The Elastic IP will be disassociated and the instance will be recreated

    Why it's wrong here

    This option incorrectly states that the Elastic IP will be disassociated. When the EC2 instance is terminated, the Elastic IP automatically becomes unassociated from it by AWS. Terraform's primary action will be to create a *new* EC2 instance, as the original is missing. Subsequently, Terraform will then *associate* the existing Elastic IP with this newly created instance, rather than performing an explicit disassociation step. The EIP itself remains provisioned in the account throughout this process.

  • Terraform will only recreate the EC2 instance without reassociating the Elastic IP

    Why it's wrong here

    Terraform will not solely recreate the EC2 instance without reassociating the Elastic IP. The `aws_eip_association` resource in Terraform's configuration explicitly defines the desired link between the Elastic IP and the EC2 instance by referencing the instance's ID. When the original instance is terminated and a new one is created, Terraform will recognize that the association is no longer valid for the new instance. Therefore, it will update or recreate the `aws_eip_association` resource to correctly point to the `id` of the newly provisioned EC2 instance, ensuring the complete desired state is achieved.

  • The apply will fail because the Elastic IP is still attached to the terminated instance

    Why it's wrong here

    The apply will not fail due to the Elastic IP being attached to a terminated instance. In AWS, an Elastic IP automatically becomes unattached from an instance when that instance is terminated; it does not remain "attached" to a non-existent resource. Terraform is designed to detect and reconcile such drift. It will identify the missing instance and the unassociated EIP, then successfully plan and execute the recreation of the instance and the reassociation of the Elastic IP without encountering a failure.

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.