Lesson 2 of 3 Resources, Data Sources and References

Data Sources

Data sources let a configuration read information it does not manage. This lesson explains how they differ from resources, when Terraform reads them and when to use them.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish data sources from resources.
  • Explain when Terraform reads a data source.
  • Choose between a data source and a resource for a given object.

Declaring and using a data source

A data block has the same two-label form as a resource block, with the keyword data. Its arguments are query constraints, and its attributes are the results. References add the data. prefix.

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"] # Canonical

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
  }
}

data "aws_caller_identity" "current" {}

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

The first data source finds the newest Ubuntu image published by Canonical; the second needs no arguments and returns details of the credentials in use. The instance references both, so Terraform reads them before creating it.

How data sources differ from resources

Terraform never creates, updates or destroys the object behind a data source; it only reads it. Data sources are recorded in state with mode = "data", but destroy ignores them. Some data sources are not lookups at all but computations performed by the provider, such as aws_iam_policy_document, which builds a JSON policy, or the http data source, which makes an HTTP request.

resource block
Terraform's roleCreates, updates and destroys the object
Referenceaws_vpc.main.id
Affected by terraform destroyYes
Lifecycle blockAll lifecycle settings
Choose when This configuration owns the object
data block
Terraform's roleReads information only
Referencedata.aws_vpc.main.id
Affected by terraform destroyNo
Lifecycle blockPreconditions and postconditions only
Choose when The object is owned elsewhere or the data is computed

The following diagram shows a shared resource managed in one configuration and read in another.

Who owns the shared VPC. The network team's configuration manages it with a resource block. Copying that block into the application configuration would make it create a second VPC, or fight over the same one if imported. A data source, or the network configuration's outputs, reads the VPC and leaves ownership with the network team.
The tag value is illustrative.
Sort into groups

Resource or data source?

Exam skills 4.1.1 · 4.1.2

Decide whether each need calls for a resource block or a data block.

Log in to try this activity Interactive activities are free with an account.

When data sources are read

Terraform reads data sources during planning, so their results are available to the plan. If a data source's arguments depend on a value that is unknown until apply, such as the ID of a resource being created in the same run, Terraform defers the read to the apply step, and the plan shows <= read (data resources) with its attributes as (known after apply). Adding depends_on to a data source has a similar effect whenever the dependency has changes, which is why explicit dependencies on data sources should be avoided.

True or false

Reading data sources

Exam skills 4.1.2

Check when Terraform reads data sources.

Log in to try this activity Interactive activities are free with an account.
Worked example

Resource or data source

An application team deploys instances into a VPC that the network team manages in its own configuration. A developer copies the network team's aws_vpc resource block into the application configuration to get its ID. What is wrong, and what should they do?

The decisive question is who owns the VPC.

A resource block tells Terraform to manage the object. The copied block would make the application configuration try to create a second VPC, or, if imported, fight the network configuration over the same object. The application team should use a data source, such as data "aws_vpc" "shared" { tags = { Name = "shared-prod" } }, or read the network configuration's outputs, which leaves ownership with the network team.

Check yourself

Does terraform destroy delete the object behind a data source?

Show answer

No. Terraform only reads data sources, so destroy never affects their objects.

Exam Tips

Remember for the exam

  • data blocks read; references start with data.
  • Data sources are read during plan unless they depend on unknown values.
  • destroy never destroys a data source's object.
  • Use a data source for objects owned by another configuration.