TF-004 Implicit vs Explicit Dependencies Practice Question
Exhibit
resource "aws_lb" "frontend" {
name = "frontend-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb.id]
subnets = ["subnet-12345678", "subnet-87654321"]
}
resource "aws_lb_listener" "frontend_http" {
load_balancer_arn = aws_lb.frontend.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.frontend.arn
}
}
resource "aws_lb_target_group" "frontend" {
name = "frontend-tg"
port = 80
protocol = "HTTP"
vpc_id = "vpc-12345678"
}Refer to the exhibit. A user applies this configuration. They then run 'terraform destroy' but the destroy fails with an error: 'Error deleting load balancer: DependencyViolation: The load balancer 'arn:aws:elasticloadbalancing:...' cannot be deleted because it is currently associated with another resource.' The user has not made any changes to the resources. What is the most likely cause?
⚠ Common exam trap
The error 'DependencyViolation' indicates that the load balancer cannot be deleted because it is still associated with a listener. Although the listener references the load balancer via `load_balancer_arn`, Terraform may not always infer this implicit dependency, especially if the reference comes from a module output or variable. Adding an explicit `depends_on` from the listener to the load balancer ensures the listener is destroyed first, releasing the association.
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 aws_lb_listener is missing an explicit depends_on for the aws_lb.
The destroy fails because Terraform attempts to delete the load balancer before the listener that is associated with it. The listener has an implicit dependency on the load balancer via the `load_balancer_arn` attribute, but Terraform may not always recognize this implicit dependency, especially if the reference is indirect. Therefore, an explicit `depends_on` from the listener to the load balancer is required to ensure the listener is destroyed first, releasing the association and allowing the load balancer to be deleted. Option D correctly identifies this missing dependency on the listener.
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 aws_lb_listener does not have explicit depends_on for the aws_lb_target_group.
Why it's wrong here
The aws_lb_listener does not require an explicit depends_on for the aws_lb_target_group. Terraform implicitly understands that the target group must exist before the listener can reference it in its default_action or rule blocks, handling the creation order. The typical deletion error occurs when the load balancer cannot be destroyed because its listeners still exist, not due to any dependency between the listener and the target group during destruction.
- ✗
The aws_lb_target_group is missing an explicit depends_on for the aws_lb_listener.
Why it's wrong here
An explicit depends_on on the aws_lb_target_group for the aws_lb_listener is unnecessary and incorrect for resolving the common DependencyViolation error during load balancer destruction. The target group is referenced by the listener, establishing an implicit creation dependency, but the target group's lifecycle does not depend on the listener's for destruction. The target group can be destroyed independently or after the listener without causing the specific error related to the load balancer's deletion.
- ✗
The aws_lb_target_group is missing an explicit depends_on for the aws_lb.
Why it's wrong here
The aws_lb_target_group does not require an explicit depends_on for the aws_lb. While a target group is eventually associated with a load balancer via a listener, the target group itself is a distinct resource that can exist independently. Terraform's implicit dependencies handle the creation order where the listener references the target group, but the load balancer's destruction does not directly depend on the target group's destruction.
- ✓
The aws_lb_listener is missing an explicit depends_on for the aws_lb.
Why this is correct
The aws_lb_listener is indeed missing an explicit depends_on for the aws_lb. AWS API rules mandate that listeners must be deleted before their associated load balancer can be destroyed. Without an explicit depends_on on the listener for the load balancer, Terraform might attempt to destroy the load balancer first, leading to a DependencyViolation error from AWS. This explicit dependency ensures the correct destruction order, preventing the error.
Go deeper
Related to this question
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 →
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.