Interview Prep/Real-World Interview/Write-Terraform tasks

Write Terraform that looks up an existing VPC by tag instead of hardcoding its ID.

IntermediateHands-onWrite-Terraform tasks
Answer
data "aws_vpc" "shared" {
  tags = { Name = "shared-prod" }
}

data "aws_subnets" "private" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.shared.id]
  }
  tags = { Tier = "private" }
}

output "private_subnet_ids" { value = data.aws_subnets.private.ids }

Key trick → Data sources read existing infrastructure without managing it. The plan fails if zero (or for aws_vpc, more than one) match, which is a useful guardrail.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-Terraform tasks