Interview Infrastructure as Code (Terraform, Ansible)

What are explicit versus implicit dependencies, and what does depends_on do?

Infrastructure as Code (Terraform, Ansible) · Basic level

Answer

Implicit dependencies come from references, such as an instance using a subnet ID. Explicit dependencies use depends_on when no data reference expresses the required ordering. depends_on should be used sparingly because overusing it makes plans less parallel and can hide weak module interfaces.

Technical explanation

References such as subnet_id = aws_subnet.private.id are preferred because they carry data and ordering.

depends_on is useful for side effects not represented in values, such as IAM propagation or module-level sequencing.

Unnecessary depends_on can make values unknown during plan and reduce parallelism.

Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.

Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.

Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.

Hands-on example

1. Practice lifecycle and dependency controls for: What are explicit versus implicit dependencies, and what does depends_on do?

2. Add lifecycle rules deliberately:

resource "aws_db_instance" "prod" {

identifier = "prod-db"

lifecycle {

prevent_destroy = true

ignore_changes = [allocated_storage]

}

}

3. Force a reviewed replacement with:

terraform plan -replace='aws_instance.web["blue"]' -out=replace.tfplan

terraform apply replace.tfplan

4. Use depends_on only when references do not express the ordering. Then run terraform graph or inspect the plan to explain the dependency path.

5. If you use -target for recovery, immediately follow with a full terraform plan.

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Infrastructure as Code (Terraform, Ansible) interview questions

← All Infrastructure as Code (Terraform, Ansible) questions