Data sources

Intermediate · 19 min

Read things Terraform did not create, like an existing VPC, an AMI or a published IP list, with data blocks, and know when the read happens and what it costs.

Before thisOutputs and localsHCL syntax: blocks, arguments and expressions

Tech Tutorials with Piyush · 23 min · from 0:28

Data sources

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Tech Tutorials with PiyushReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `data` block reads existing information and exposes it as attributes, `data.<type>.<name>.<attr>`. It creates and manages nothing, and destroy does not touch what it read.
  • Use data sources for things outside this configuration's ownership: the latest AMI, a VPC another team made, an account ID, a published IP list.
  • Terraform reads data sources during plan when it can. If the arguments are unknown until apply, the read is deferred to apply.
  • A data source that finds nothing, or several things when it expected one, fails the plan with a clear error.
  • Reading a resource by data source is a loose link. If you own both pieces, reference the resource or use remote state, and keep data sources for what you truly do not manage.

Not everything is yours

The configuration you have written so far creates everything it uses. Real accounts are not like that. The VPC was made years ago by the network team. The newest approved machine image changes every month and you do not want to paste an ID that goes stale. The AWS account ID is a fact, not something you build.

A data source reads existing facts and hands them to the rest of your code. In the cloud, “read” is the right verb. In Terraform’s vocabulary, a resource is something you own and manage. A data source is something you only look at.

The shape

Written from the provider docs and not run:

data "aws_vpc" "shared" {
  tags = {
    Name = "shared-network"
  }
}

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

resource "aws_instance" "app" {
  subnet_id = data.aws_subnets.private.ids[0]
  # ...
}

You read it as data.<type>.<name>.<attribute>. The first lookup finds a VPC by its tag. The second finds its private subnets, using the first as input. The resource uses the result. Data sources take arguments and expose attributes, just like resources, and they take part in the dependency graph the same way. data.aws_subnets.private waits for data.aws_vpc.shared.

Reading a real one

To see what a data source is actually doing, look at one that needs no cloud account. The AWS provider has aws_ip_ranges, whose docs describe it as returning the IP ranges of AWS products and services, with attributes including cidr_blocks, create_date and sync_token. Its docs point at AWS’s published IP address ranges file. I fetched that file and did the same filter the docs’ example does, regions eu-west-1 and eu-central-1, service ec2:

$ curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq -c '{create_date:.createDate, sync_token:.syncToken, cidr_blocks:[.prefixes[]|select((.region=="eu-west-1" or .region=="eu-central-1") and .service=="EC2")|.ip_prefix]|length}'
{"create_date":"2026-11-27-00-47-05","sync_token":"1795740425","cidr_blocks":174}

174 CIDR blocks for those two regions today. This is jq doing the filtering, not the provider, and I did not run Terraform, so I do not claim the data source returns exactly this. The file changes whenever AWS adds ranges, which is why the data source exists. Pasting the 174 blocks into a security group by hand would be stale next week.

When it is read

The read is part of plan. Terraform queries the data source, shows nothing to change for it, and uses the values to plan the rest. If an argument to the data source depends on a resource that does not exist yet, Terraform cannot read it during plan. It defers the read until apply, and everything derived from it shows (known after apply), as in the plan-reading lesson. That has a side effect: values from deferred data sources cannot be used for count or for_each keys, because those must be known at plan time.

A data source is read every plan, not once. A change in the real world, such as a new AMI, shows up in the next plan as a diff in whatever uses it. That is a feature if you want “latest”, and a hazard if you do not. With an AMI lookup for the latest image, a plan next month may propose replacing your servers while you changed no code. Pin by name and version when you want stability.

Errors you will see

  • No result. A lookup that finds nothing fails the plan. This usually means the wrong account, wrong region or a typo in a tag.
  • Too many results. Many data sources expect exactly one match. Several matches is an error, not a pick-the-first. That is a good thing, as the check question says.

The caution

Data sources are convenient and create invisible coupling. The code above works only if a VPC tagged shared-network exists in the account and region you are running against. Nothing records that dependency in the repository, and nothing breaks until someone renames a tag. When both pieces are yours, prefer an explicit link, an output from one configuration read through terraform_remote_state or the module’s output passed in as a variable. Reserve data sources for what you truly do not manage, or what is global fact: account ID, regions, availability zones, published ranges, and the latest approved image.

About the video

The embed is Tech Tutorials with Piyush’s “13/30 - Terraform Data Source AWS Explained (with demo)”, December 2025, from “What are data sources” to just before the conclusion (0:28 to 19:37). The chapters show fetching an existing VPC, an existing subnet and an AMI lookup. It is part of a 30-day series with an English caption track, an Indian-English presenter, and an AWS demo that needs an AWS account to follow along. I judged it from its description and chapters. Rahul Wagh’s 2021 video in deeper covers the same ground with an EC2 demo.

Where this fits

Resources and data sources now form a graph. The last lesson in this group looks at the edges you write yourself: dependencies, implicit and explicit.

Check yourself

  1. 01What is the difference between `resource "aws_vpc" "main"` and `data "aws_vpc" "main"`?

    The resource creates and manages a VPC and records it in state. The data source only looks up an existing one and exposes its attributes. Destroying the configuration deletes the first and leaves the second alone.

  2. 02A data source lookup by tag matches two subnets and the plan fails. Why is that useful?

    The error forces a decision. Silently taking the first match would let infrastructure change when someone adds a similar subnet. Make the filter specific enough to match exactly one.

  3. 03When does Terraform read a data source?

    During plan, when all its arguments are known then. If an argument depends on something only known after apply, the read happens at apply and values derived from it show as known after apply.

  4. 04Your configuration builds a VPC and, in a different folder, a team looks up that VPC with a data source by tag. What is fragile about it?

    Nothing ties them together. Renaming the tag, or running in the wrong account or region, silently finds a different VPC or none. An explicit output or remote state reference is a firmer contract.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark