Skip to main content
In this lesson we cover Terraform resource referencing — a core concept for building dynamic, connected infrastructure. Resource referencing lets one block consume attributes from another (for example, a subnet ID or DNS zone), which reduces hard-coded values and makes configurations easier to maintain and evolve. Why this matters:
  • Create dynamic configurations that adapt when underlying resources change.
  • Let Terraform infer ordering via its dependency graph instead of manually sequencing operations.
  • Reference resources unambiguously using their addresses (for example, aws_vpc.my_vpc).
A short HCL example illustrating block structure:
Below is a conceptual diagram showing networks at the top and consumers (firewall, VM, Kubernetes cluster, DNS record) that depend on network/data lookups.
A purple infographic titled "Resource Referencing" that explains Terraform concepts and benefits, with bullet points like dynamic configurations, automatic dependency mapping, and resource identifiers. The right side shows a flow diagram of resource dependencies (Network Configuration, Firewall, Virtual Machine, Kubernetes Cluster, DNS Record, and Data Lookup).
How references and data sources interact
  • Use data blocks to read existing resources (for example, an existing DNS zone or VPC).
  • Use attributes from data or resource blocks as inputs to other resources.
  • Terraform ensures data lookups occur before resources that depend on them, so the correct values are available during planning and apply.
For example, you might use a data source to find a DNS zone, then pass an attribute like zone_id into a record:
  • Data source fetches current state (account, region, external network).
  • Resources use those attributes to create or manage dependent objects.
  • Changes ripple through the dependency graph so Terraform knows what to plan and apply.
In larger projects you’ll commonly use multiple block types: resource, data, variable, output, provider, etc. The diagram below summarizes these relationships and how they form Terraform’s internal dependency graph.
A slide titled "Resource Referencing" showing a flowchart of purple boxes labeled "Resource", "Data", "Output", and "Variable" with arrows between them to illustrate dependency relationships (Terraform).
Quick HCL demo and style guidance
  • File extensions: .tf.
  • Format consistently with terraform fmt to follow the canonical HCL style.
  • Resource addresses follow <type>.<name> (for example, github_repository.production-repo).
  • Use an editor with HCL support (for example, VS Code with the HashiCorp Terraform extension) to get block scaffolding and syntax highlighting.
Example: creating a GitHub provider and repository (well-formatted):
Editor tips
  • Scaffolding often suggests provider and resource blocks.
  • Run terraform fmt after editing to align equals signs and standardize layout across all .tf files in the working directory.
Unique local names and addressing Terraform identifies each resource by its address: <type>.<local_name>. Within the same resource type, local names must be unique. Invalid (duplicate local names):
Valid — distinct local names for multiple repositories:
Note: you may reuse the same local name across different resource types because the full address includes the type (for example, github_user.production-repo vs github_repository.production-repo).
Do not use the same local name for multiple resources of the same type. Terraform will raise a duplicate address error during planning. Use descriptive, unique local names to avoid ambiguity in large configurations.
Splitting resources into multiple files Terraform treats all .tf files in a single working directory as one combined configuration. Splitting resources into logical files improves readability and maintainability. Example layout: github.tf
test.tf
Formatting multiple files
  • Running terraform fmt in the working directory formats all .tf files there.
  • If files are already formatted, terraform fmt only reports the files it changed.
Example terminal interactions:
If only github.tf needed formatting:
Useful references Quick reference table Summary
  • Use resource referencing to avoid hard-coded values and to let Terraform compute dependencies automatically.
  • Give each resource a unique local name within its type to avoid address collisions.
  • Organize resources into multiple .tf files for clarity; Terraform evaluates them as a single configuration.
  • Enforce consistent formatting with terraform fmt.
Use resource references to create clear, maintainable relationships between blocks, and rely on Terraform’s dependency graph instead of manual ordering.

Watch Video