interview-prep

Crisp answer: Terraform builds a dependency graph from explicit references between resources. Resources that reference each other are automatically sequenced. For cases with no direct reference, use depends_on to enforce ordering.

Implicit dependencies:

When one resource references another's attribute, Terraform understands the dependency automatically:

resource "aws_security_group" "app" {
  name   = "app-sg"
  vpc_id = aws_vpc.main.id    # ← implicit dep on aws_vpc.main
}

resource "aws_instance" "web" {
  ami                    = "ami-12345"
  instance_type          = "t3.micro"
  vpc_security_group_ids = [aws_security_group.app.id]  # ← implicit dep
  subnet_id              = aws_subnet.private.id         # ← implicit dep
}

Terraform will create the VPC first, then the security group (which needs the VPC ID), then the instance (which needs the security group ID and subnet ID). This ordering is automatic.

Explicit dependencies with depends_on:

When there is no direct attribute reference but one resource still depends on another being ready first:

resource "aws_iam_role_policy_attachment" "node" {
  role       = aws_iam_role.node.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
}

resource "aws_eks_node_group" "workers" {
  cluster_name  = aws_eks_cluster.main.name
  node_role_arn = aws_iam_role.node.arn

  # The node group creation fails if the policy attachment is not done first,
  # but there is no direct attribute reference between them:
  depends_on = [
    aws_iam_role_policy_attachment.node,
    aws_iam_role_policy_attachment.cni,
    aws_iam_role_policy_attachment.ecr,
  ]
}

Viewing the dependency graph:

terraform graph | dot -Tsvg > graph.svg
# Requires graphviz installed: brew install graphviz

Parallelism:

Terraform runs independent resources in parallel (default 10 concurrent operations). Resources with dependencies run sequentially in dependency order.

terraform apply -parallelism=20   # Increase for large infra
terraform apply -parallelism=1    # Serial execution (debugging)

What to say in the interview:

"Terraform builds a directed acyclic graph of resources based on attribute references. If resource B uses resource A's ID, Terraform knows to create A first automatically. For cases where there's no direct reference but ordering still matters — like IAM policy attachments that must complete before an EKS node group starts — you use depends_on explicitly. The graph also enables parallel execution: independent resources are created concurrently, which significantly speeds up large apply operations."


My notes