| Concept | Why it matters | Example |
|---|---|---|
| Dynamic configuration | Avoid hard-coding values; reuse attributes emitted by one resource in another | A VM references aws_subnet.example.id instead of a literal subnet ID |
| Dependency mapping | Terraform infers create/update/delete order from references, ensuring resources are provisioned in the correct sequence | A load balancer depends on backend instances referenced in its configuration |
| Unique identification | Each block has a resource type + local name (address) that other blocks use to reference it | github_repository.production-repo uniquely addresses that repo resource |

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
nameto create records. - Query a cloud account to obtain a subnet or image ID that your resource needs.
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.

terraform fmt). The focus is on writing, referencing, and formatting HCL rather than provider-specific behavior.
-
Create a file named
github.tfin your working directory. VS Code with a Terraform extension will provide syntax highlighting and snippets forproviderandresourceblocks. - Add a provider block that references a token variable:
- Define a repository resource. Each resource block is a combination of
typeandlocal namethat forms the unique address used elsewhere in the configuration:
- 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.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:
.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:
terraform fmt in the directory will scan and format all .tf files and report each file it modified.
Quick best practices
| Area | Recommendation |
|---|---|
| Referencing | Prefer using attributes from created resource or data blocks instead of hard-coded values |
| Secrets | Use variables and secure secret stores — avoid committing tokens to VCS |
| Formatting | Run terraform fmt regularly or enable automatic formatting in your editor |
| Organization | Group related resources into separate .tf files or modules for maintainability |
- 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.
- Terraform Documentation
- Terraform CLI — terraform fmt
- Terraform GitHub Provider
- HCL Language Documentation