Resources, data sources, and references
Terraform 004 · Lesson 6 of 18 · 45:10
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: resource blocks and the lifecycle of the objects that they manage, data sources, references to attributes, and resource addresses, including instances created with count and for_each. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Read it or manage it
Bluefin's tracking servers need the newest Ubuntu image, which Bluefin does not manage, and a security group, which Terraform must create. Terraform reads the first, and manages the second. This lesson covers resource blocks, data sources, references between them, and the addresses that Terraform gives them.
Resource blocks
A resource block declares an infrastructure object that Terraform manages: a server, a network, a bucket, or a DNS record. It starts with the keyword resource, then two labels: the resource type, defined by the provider, and a local name. Together, the type and the name identify the resource, so they must be unique within a module. Inside the block, arguments set the configuration of the object, and the provider documentation lists them for each type. Terraform manages the whole lifecycle of the object: it creates it, updates it when the arguments change, and destroys it when the block is removed.
Data sources
A data source lets Terraform use information about something that it does not manage: an image published by a vendor, an existing network, or an object from another configuration. It is declared with a data block, with a type, a local name, and arguments that filter what to look up. A data source is read only: Terraform fetches its attributes, and never creates, changes, or deletes anything. Terraform reads it during the plan when it can, and during the apply when its arguments depend on values that are not known yet. So the newest Ubuntu image comes from a data source, and the security group from a resource block.
References and attributes
To use a value from another block, you write a reference: the resource type, the name, and an attribute, separated by dots. For a data source, add the word data in front, and other blocks have their own prefixes, like var for variables, local for local values, and module for module outputs. Attributes include the arguments that you set, and values that the provider sets after creation, like an ID, which the plan shows as known after apply. Every reference also creates an implicit dependency: Terraform creates the referenced object first, then the object that uses it. So you rarely need to declare the order yourself.
Resource addresses
Terraform gives every resource an address, which the CLI uses to point at one object. The basic address is the type and the name, like AWS instance dot web, and the address of a data source starts with data. With count, each instance gets an index in square brackets, starting at zero, and with for each, a key in quotes. Inside a module, the address starts with module, then the module name, like module dot app dot AWS instance dot web. You use these addresses in commands like terraform state show, and with the replace option of plan and apply.
Recap
Let's recap. A resource block declares an object that Terraform manages, with a type from the provider and a local name. A data source reads information about something that Terraform does not manage, and changes nothing. A reference uses the type, the name, and an attribute, with prefixes like data, var, and module, and it creates an implicit dependency. And an address points at one object, with an index for count, a key for for each, and a module prefix inside modules.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Bluefin's configuration contains resource "aws_instance" "web". What is web?
- The Name tag of the instance in AWS
- A local name, used in Terraform
- The type defined by the provider
- The hostname of the server
Show the answer
A local name, used in Terraform
The second label is a local name, which refers to this resource elsewhere in the same module, and has no meaning outside Terraform. “The Name tag of the instance in AWS” is wrong, because a name tag in AWS would be set with the tags argument. “The type defined by the provider” is the first label, AWS instance. And “The hostname of the server” is wrong, because the hostname of a server comes from its own arguments, or from the operating system.
An instance refers to aws_security_group.api.id in its arguments, and no depends_on is set. In what order does Terraform create them?
- Both at the same time, in parallel
- The instance first, then the group
- It stops and asks for depends_on
- The group first, then the instance
Show the answer
The group first, then the instance
The reference is an implicit dependency, so Terraform creates the security group, then the instance that uses its ID. “Both at the same time, in parallel” is wrong, because Terraform only runs independent operations in parallel. “The instance first, then the group” is wrong, because the instance needs an ID that does not exist yet. And “It stops and asks for depends_on” is wrong, because Terraform needs no depends on argument when a reference already gives the order.
Bluefin looks up a VPC with data "aws_vpc" "main". How should a subnet refer to the ID of that VPC?
- data.aws_vpc.main.id
- aws_vpc.main.id
- module.aws_vpc.main.id
- data.main.id
Show the answer
data.aws_vpc.main.id
References to a data source start with data, then the type, the name, and the attribute. “aws_vpc.main.id” would refer to a managed resource, and this configuration has none with that name. “module.aws_vpc.main.id” would point to an output of a module called AWS VPC, which does not exist. And “data.main.id” is missing the type, which every reference needs.