⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 29 of 120 in Terraform & IaC
Senior DevOps / SRE [L2] Terraform Terraform Modules & State Production Scenario [L2]
🎯 Target Role / Context: Terraform Scenario #24 (L2) · Use VPC ID from another module

Q: What is a `data` source in Terraform?

A data source in Terraform is a read-only lookup, declared with a data block, that fetches information about something Terraform does not manage in the current configuration, such as an existing VPC, the latest AMI or the current.

#Terraform #L2 #IaC #Cloud Infrastructure
🎙️ Candidate Opening & Architectural Context
"A <strong>data source in Terraform</strong> is a read-only lookup, declared with a <code>data</code> block, that fetches information about something Terraform does not manage in the current configuration, such as an existing VPC, the latest AMI or the current AWS account ID. Resources create and own infrastructure; data sources only read it. Interviewers ask this to check that you can integrate with existing infrastructure and other teams stacks without hard-coding IDs, and that you understand when the read happens, what happens if nothing matches and how data sources differ from variables and remote state. The answer should include a real example and the failure modes."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: A <strong>data source in Terraform</strong> is a read-only lookup, declared with a <code>data</code> block, that fetches information about something Terraform does not manage in the current configuration, such as an existing VPC, the latest AMI or the current AWS account ID. Resources create and own infrastructure; data sources only read it. Interviewers ask this to check that you can integrate with existing infrastructure and other teams stacks without hard-coding IDs, and that you understand when the read happens, what happens if nothing matches and how data sources differ from variables and remote state. The answer should include a real example and the failure modes.

1️⃣

Mental model: data blocks versus resource blocks

The distinction is ownership: Terraform manages the lifecycle of resources and only queries data sources.

  • Resource: resource "aws_instance" "web" creates, updates and destroys a real object, and tracks it in state.
  • Data source: data "aws_vpc" "main" asks the provider API for existing information. Terraform never creates or deletes the thing it reads.
  • Reference syntax: read values with the data. prefix, for example data.aws_vpc.main.id.
  • State: results are stored in state so plans can use them, but they are refreshed on each plan and apply, so they reflect current reality.
  • Why interviewers care: data sources are how Terraform composes with infrastructure created by other teams, the console or a different state, without importing it.
2️⃣

Hands-on: using a data source in Terraform

Typical uses are looking up AMIs, networks, account context and outputs from another state.

data "aws_caller_identity" "current" {}

data "aws_ami" "al2023" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["al2023-ami-2023*-x86_64"]
  }
}

data "aws_vpc" "main" {
  tags = { Name = "prod-vpc" }
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.al2023.id
  instance_type = "t3.micro"
  tags = { Owner = data.aws_caller_identity.current.account_id }
}

# Inspect what Terraform read
# terraform state list | grep '^data\.'
# terraform console   ->   data.aws_vpc.main.id
  • Filter precisely: an aws_ami data source needs owners and filter blocks, otherwise it may match nothing or the wrong image.
  • Single match required: many data sources error if more than one result is found; use arguments like most_recent = true where offered.
  • Cross-stack: terraform_remote_state reads outputs from another state, though published outputs or SSM parameters create looser coupling.
  • Account context: aws_caller_identity and aws_region avoid hard-coding account IDs in ARNs.
  • Use with for_each: a data source can itself use for_each, for example to look up several subnets by ID.
Advertisement
3️⃣

When data sources are read and how to verify

Reads happen at plan time when arguments are known, otherwise they are deferred until apply.

data "aws_subnet" "private" {
  id = var.subnet_id
  lifecycle {
    postcondition {
      condition     = self.map_public_ip_on_launch == false
      error_message = "Subnet must be private."
    }
  }
}
  • Known arguments: the lookup runs during plan, so you can review the resolved value in the plan output.
  • Unknown arguments: if an argument depends on a resource not yet created, the read is deferred to apply and values show as (known after apply).
  • Validate results: use precondition and postcondition blocks in lifecycle (Terraform 1.2 and later) to assert a data source returned what you expect.
  • Quick check: terraform console evaluates expressions such as data.aws_ami.al2023.name against real state.
  • Refresh behaviour: terraform plan -refresh-only shows how data source results changed without proposing infrastructure edits.
4️⃣

Failure modes and best practice

Data sources are convenient, but they introduce hidden dependencies on infrastructure you do not control.

  • Nothing found: the plan fails with "no matching ... found". Check filters, region, account and permissions, since read permissions are also needed.
  • Drift of inputs: a most_recent AMI lookup can change on any plan and trigger instance replacement. Pin AMI names, or use SSM parameters, when stability matters.
  • Ordering: a data source that must read something created in the same run needs depends_on, which can defer the read to apply; prefer passing resource attributes directly.
  • Secrets in state: reading a secret value into a data source stores it in state, so protect and encrypt the backend.
  • Alternatives: if a value is a fixed setting rather than discoverable infrastructure, a variable is clearer than a lookup.
Pro Tip: Use a data source for things owned elsewhere. If your configuration owns the object, reference the resource attribute instead.
5️⃣

Common data source patterns in real projects

A few recurring patterns cover most real use.

  • Existing network: look up a shared VPC and subnets by tag so application stacks do not hard-code IDs that differ per environment.
  • IAM policy documents: aws_iam_policy_document builds JSON policies in HCL; it reads nothing remote but is a data source used for rendering.
  • Secrets and parameters: aws_ssm_parameter reads published values from another team; mark outputs sensitive and remember state storage.
  • Zones and availability: aws_availability_zones yields valid zones per region so modules stay portable.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Prefer stable identifiers (names, tags, SSM parameters) in data source filters and add postconditions, because a most_recent lookup can silently trigger replacements on the next plan."
⚡ 60-Second Elevator Pitch Talking Points
  • A data source in Terraform is a read-only data block that looks up existing infrastructure or context, as opposed to a resource that Terraform creates and owns.
  • I verify it with terraform plan, terraform console and state list, and add pre or postconditions to assert the result.
  • I automate by replacing hard-coded IDs with data lookups and by pinning AMIs or reading published SSM parameters for cross-stack values.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Using a data source for something the same configuration creates Reference the resource attribute directly; a lookup adds an extra API call and can defer reads to apply time.
  2. Loose filters that match multiple objects Many data sources fail on multiple matches, and others pick an unexpected one, leading to wrong AMIs or subnets.
  3. Relying on most_recent for production AMIs A new image publication changes the plan and can force instance replacement without any code change.
  4. Forgetting the read permissions of the CI role Data sources call real provider APIs, so a role with write-only scope fails during plan.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →