Course contentsShow
← Cloud and Infrastructure
Lesson 746 of 2,98914. Terraform FundamentalsFree lesson

Resource Addressing and References

Reference resource attributes using the resource_type.name.attribute syntax to create dependencies.

What You'll Learn

You'll learn how to reference one resource's output values inside another resource's configuration, creating automatic dependencies in Terraform.

Why Resource References Matter

When building infrastructure, resources often depend on each other. A web server needs to know the database's connection string. A load balancer needs the IDs of the instances it distributes traffic to. Rather than hardcoding these values (which would break automation), Terraform lets you reference resource attributes dynamically.

How Resource References Work

Terraform uses a simple addressing syntax to reference any resource's attributes:

resource_type.name.attribute
  • resource_type: The provider resource type (e.g., aws_instance)
  • name: The unique name you gave this resource in your config
  • attribute: The specific output value from that resource

Example:

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

resource "aws_eip" "web_ip" {
  instance = aws_instance.web.id
}

Here, aws_instance.web.id references the id attribute of the web instance. Terraform automatically knows the Elastic IP depends on the instance being created first.

Think of It Like Spreadsheet Formulas

If resource A is cell B2 and resource B is cell C2, writing = B2 in C2 creates a dependency. Change B2, and C2 updates automatically. Terraform references work the same way—change the instance, and anything referencing it knows to update.

Implicit Dependencies

When you use a reference, Terraform creates an implicit dependency—it automatically determines the creation order. You don't need depends_on for this; the reference itself establishes the relationship.

Key Takeaway:
Use resource_type.name.attribute syntax to reference resource outputs and create automatic dependencies between infrastructure components.