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.
# single-line comment
block_type "block_label" "block_label" {
  first_argument  = expression or value
  second_argument = expression or value
  third           = expression or value
}

attribute_abc = "value_1"
attribute_2   = "value_2"
Core concepts at a glance
ConceptWhy it mattersExample
Dynamic configurationAvoid hard-coding values; reuse attributes emitted by one resource in anotherA VM references aws_subnet.example.id instead of a literal subnet ID
Dependency mappingTerraform infers create/update/delete order from references, ensuring resources are provisioned in the correct sequenceA load balancer depends on backend instances referenced in its configuration
Unique identificationEach block has a resource type + local name (address) that other blocks use to reference itgithub_repository.production-repo uniquely addresses that repo resource
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:
provider "github" {
  token = var.github_token
}
  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:
resource "github_repository" "production-repo" {
  name        = "prod-repo"
  description = "Repo for our production app"
  private     = true
}
  1. Add another repository using a different local name so both resources have unique addresses:
resource "github_repository" "testing-repo" {
  name        = "test-repo"
  description = "Repo for our testing app"
  private     = true
}
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:
$ terraform fmt
github.tf
test.tf
  • If only one file required formatting, the output might be:
$ terraform fmt
github.tf
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:
resource "github_repository" "testing-repo" {
  name        = "test-repo"
  description = "Repo for our testing app"
  private     = true
}
github.tf:
provider "github" {
  token = var.github_token
}

resource "github_repository" "production-repo" {
  name        = "prod-repo"
  description = "Repo for our production app"
  private     = true
}
Running terraform fmt in the directory will scan and format all .tf files and report each file it modified. Quick best practices
AreaRecommendation
ReferencingPrefer using attributes from created resource or data blocks instead of hard-coded values
SecretsUse variables and secure secret stores — avoid committing tokens to VCS
FormattingRun terraform fmt regularly or enable automatic formatting in your editor
OrganizationGroup related resources into separate .tf files or modules for maintainability
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