Skip to main content
Welcome back. In this lesson we’ll explore Terraform data sources — read-only lookups that let your configuration query existing infrastructure instead of creating it. Data sources make Terraform configurations more flexible and safer by dynamically retrieving values (IDs, ARNs, CIDRs, endpoints, etc.) from cloud providers or other platforms and exposing those attributes for use in resources, variables, and outputs. By the end of this article you’ll understand what data sources are, how they work, and how to use them across common providers (AWS, Azure, Kubernetes, VMware) to avoid hard-coded values and enable dependable dependency resolution.

What is a data source?

A data source is a Terraform block that queries provider APIs to return attributes of an existing resource. Unlike resource blocks, data blocks do not create or modify infrastructure — they only read and expose information. Common uses:
  • Look up an existing VPC by tag or name to obtain its ID and CIDR.
  • Query an Azure Kubernetes Service (AKS) cluster and expose its resource ID or kubeconfig.
  • Retrieve VMware datacenter, datastore, or cluster information for VM provisioning.
  • Read managed or external resources that your deployment must reference.
Example: mixing resource creation with a data lookup
Note: The data block above is read-only; Terraform will not create or modify the AKS cluster when you run plan or apply.

When data sources are useful

  • Hybrid and multi-environment setups where some resources already exist.
  • Environments managed outside Terraform (cloud console, other teams).
  • Dynamic resource creation that depends on attributes of existing resources (IDs, subnets, endpoints).
  • Preventing configuration drift and fragile hard-coded identifiers.

Example: Azure, VMware, and AWS scenarios

  • Azure: Look up an AKS cluster that already exists so you can deploy workloads or configure RBAC without recreating the cluster.
  • VMware: Query the correct datacenter, cluster, or datastore to target VM provisioning dynamically across environments.
  • AWS: Look up an existing VPC (by tag or name) to obtain vpc_id and cidr_block when creating subnets.
Networks and IP addressing are especially important. For example, when creating subnets in AWS you must provide the VPC ID. If you only know the VPC name, a data source can resolve the correct ID and CIDR block and prevent hard-coding.
A purple-themed architecture diagram showing a Terraform configuration that looks up an existing VPC and defines three subnet resources. On the right is an AWS Cloud illustration of the VPC with three availability zones, each containing a subnet.
Example: find the VPC and create subnets using values from the data source
This pattern avoids hard-coding the VPC ID or CIDR and ensures the plan/apply phases use live values returned by the provider API.

Why use data sources?

  • Read-only: they never create or modify resources.
  • Enable implicit dependency resolution when a new resource relies on an existing one.
  • Reduce hard-coded values (IDs, ARNs, CIDRs), lowering error-proneness and drift.
  • A single data source can be referenced multiple times across a configuration.
Data sources are read-only lookups that allow Terraform to discover existing infrastructure and use those attributes in resource creation or outputs.

Data source syntax and examples

Every data source starts with the data keyword, followed by two labels and a block body:
  • First label: provider resource type (for example, aws_vpc, azurerm_resource_group, kubernetes_namespace).
  • Second label: a local identifier you choose to reference the lookup elsewhere.
  • Body: arguments or filters supported by the provider for that data source (these vary per provider).
Examples:
These blocks instruct Terraform to query the provider API and populate attributes that you can reference.

Referencing attributes

Use the pattern: data.<type>.<name>.<attribute> Examples for data.aws_vpc.prd:
  • ARN: data.aws_vpc.prd.arn
  • CIDR block: data.aws_vpc.prd.cidr_block
  • ID (VPC ID): data.aws_vpc.prd.id
  • Owner ID: data.aws_vpc.prd.owner_id
A data source can return many attributes. Check the provider documentation for the exact list. Representative JSON-style output from data.aws_vpc.prd:
Use data.aws_vpc.prd.id inside a subnet resource to reference the VPC ID, or output data.aws_vpc.prd.cidr_block to expose the VPC CIDR.

Best practices and tips

  • Use clear, concise data source labels (e.g., prd, dev, shared_vpc) to make intent obvious.
  • Prefer filters or exact identifiers so the data source returns a single resource when required.
  • Reuse a single data source across your configuration rather than repeating lookups.
  • Consult the provider’s documentation for supported arguments and returned attributes.
  • When matching by tag or name, be explicit to avoid ambiguous results that return multiple resources.
Table: quick checklist

Wrap-up

Data sources let Terraform read and reference existing infrastructure so your configurations remain dynamic, maintainable, and less error-prone. Use data sources to fetch existing resources and reference their attributes when creating new resources, wiring outputs, or enabling cross-team reuse. Further reading and references:

Watch Video