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

Resource Attributes and References

Accessing resource attributes after creation and referencing them in other resources using interpolation.

Resource Attributes and References

You'll learn: How to access data from created resources and use those values in other parts of your Terraform configuration.

What Are Resource Attributes?

When Terraform creates a resource, that resource generates attributes—pieces of information about itself that you might not know until after creation. Think of it like ordering a package online: you specify what you want (the resource block), but you don't get the tracking number until after the order is placed. That tracking number is an attribute.

For example, when you create a virtual machine, you specify the size and region, but the cloud provider assigns an IP address—that's an attribute Terraform captures after creation.

Referencing Attributes in Other Resources

Once a resource exists, you can reference its attributes using interpolation syntax: <resource_type>.<resource_name>.<attribute_name>. This creates a dependency chain, ensuring Terraform creates resources in the correct order.

resource "aws_instance" "web_server" {
  ami           = "ami-12345"
  instance_type = "t2.micro"
}

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

Here, aws_instance.web_server.id references the id attribute that AWS assigns after creating the instance. Terraform automatically knows to create the instance before the Elastic IP.

Why This Matters

This referencing system lets your infrastructure components "talk" to each other. You don't need to manually copy-paste IDs between configurations or hard-code values that don't exist yet.

Key Takeaway: Resource attributes are values generated during creation; you reference them using resource_type.name.attribute to wire resources together and establish automatic dependency ordering.