Skip to main content
Explore resource referencing in Terraform — a key capability for building dynamic, interconnected infrastructure. This guide explains the core concepts, shows a simple HCL demo in VS Code, and demonstrates formatting and file organization best practices.
Core concepts at a glance
The image explains resource referencing in HashiCorp Terraform, highlighting its benefits in creating dynamic configurations, automatic dependency mapping, and resource identification. It includes a diagram illustrating the flow between network configurations, firewalls, virtual machines, Kubernetes clusters, DNS records, and data lookup against cloud providers.
Data sources In addition to resources you create, Terraform can consume external data via data blocks. Data sources let you query existing infrastructure or provider metadata and then reference those results just like attributes from created resources. Example use cases:
  • Lookup an existing DNS forward lookup zone and use its name to create records.
  • Query a cloud account to obtain a subnet or image ID that your resource needs.
Resource webs and dependency graphs As configurations grow, they form a graph of interconnected blocks — resource, data, output, variable, and so on. Terraform uses these references to compute the dependency graph and to decide the correct apply/destroy ordering and what must be updated together.
The image is a flowchart titled "Resource Referencing: The Reality of Terraform," displaying interconnected nodes labeled as "Resource," "Data," "Output," and "Variable." It illustrates the dependencies and relationships between different elements in Terraform infrastructure, using arrows to indicate connections.
HCL demo: writing Terraform files in VS Code This demo shows practical HCL examples and recommended workflow items (like using terraform fmt). The focus is on writing, referencing, and formatting HCL rather than provider-specific behavior.
  1. Create a file named github.tf in your working directory. VS Code with a Terraform extension will provide syntax highlighting and snippets for provider and resource blocks.
  2. Add a provider block that references a token variable:
  1. Define a repository resource. Each resource block is a combination of type and local name that forms the unique address used elsewhere in the configuration:
  1. Add another repository using a different local name so both resources have unique addresses:
Every resource instance in a Terraform configuration must have a unique address: the combination of its type and its local name (for example, github_repository.production-repo). Reusing the same local name for two instances of the same resource type will produce a configuration error.
Do not hard-code sensitive values (like provider tokens) directly in .tf files. Use input variables, terraform.tfvars, or environment variables (for example, TF_VAR_github_token) and store secrets in a secure secrets manager or CI/CD secret store.
Using terraform fmt to format HCL Keep code readable and consistent with terraform fmt. It normalizes indentation and aligns assignment operators to Terraform’s canonical style. Examples:
  • Run the formatter across the working directory:
  • If only one file required formatting, the output might be:
Splitting resources across files Terraform treats all .tf files in a directory as a single configuration. Use multiple files to organize resources logically — e.g., separate providers, networking, compute, and test resources. Example file split: test.tf:
github.tf:
Running terraform fmt in the directory will scan and format all .tf files and report each file it modified. Quick best practices Wrap-up
  • Resource referencing enables dynamic, maintainable Terraform configurations by passing values between blocks rather than hard-coding.
  • Terraform uses references to build a dependency graph and determine the correct provisioning order.
  • Maintain consistent style with terraform fmt, split files for clarity, and keep secrets out of source files.
Links and references

Watch Video