What is a data source?
A data source is a Terraform block that queries provider APIs to return attributes of an existing resource. Unlikeresource 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.
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_idandcidr_blockwhen creating subnets.

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 thedata 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).
Referencing attributes
Use the pattern: data.<type>.<name>.<attribute> Examples fordata.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
data.aws_vpc.prd:
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.
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:- Terraform documentation: https://www.terraform.io/docs
- AWS VPC data source docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc
- AzureRM data sources: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources
- Kubernetes provider data sources: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources