Terraform Infrastructure as CodeTerraformTerraform Data Sources

Article & Tutorial

Terraform Data Sources Explained: Read Existing Cloud Resources

Learn Terraform data sources, how they read existing cloud resources, when to use them, common examples, mistakes, and best practices.

Infrastructure code reading existing cloud resources through a data lookup layer
Infrastructure code reading existing cloud resources through a data lookup layer
Contents

Terraform Data Sources Explained

Learn how Terraform data sources read existing infrastructure, lookup cloud resources, and connect reusable Terraform code with real environments.

Quick answer

Resource

Creates or manages infrastructure.

Data source

Reads existing information from a provider.

What is a Terraform data source?

A data source lets Terraform read information about existing resources without managing their full lifecycle.

Example

data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] } }

Using the data source

resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id instance_type = "t3.micro" }

Common use cases

  • Find latest AMI/image.
  • Read existing VPC/subnet.
  • Lookup DNS zone.
  • Read existing key vault or secret reference.
  • Fetch Kubernetes cluster information.

Common mistakes

  • Using data sources when Terraform should manage the resource.
  • Using broad filters that match the wrong resource.
  • Not understanding that data sources are read during plan/apply.

Best practices

  • Use precise filters.
  • Name data sources clearly.
  • Document externally managed resources.
  • Avoid relying on unstable lookup results for production.

Related guides

  • Terraform Learning Path
  • Terraform Providers Explained

Official references

  • Terraform data sources documentation

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.