Terraform 10: Arguments vs attributes & cross-resource references
easy⏱ 5 mincourseterraform
Arguments you set, attributes you read
Arguments are values you assign (name, location). Attributes are values the provider exposes after (or during) creation — some you also set, but many are computed, like a resource's id. You can only reference an attribute after declaring the resource that produces it; the reference syntax is TYPE.LOCALNAME.ATTRIBUTE.
resource "azurerm_resource_group" "orders" {
name = "myorg-orders-dev-rg" # argument (you set)
location = "westus2" # argument (you set)
}
# After apply, azurerm_resource_group.orders exposes:
# .id → computed attribute (the full Azure resource ID)
# .name → "myorg-orders-dev-rg"
# .location → "westus2"
Referencing an attribute in another resource
Instead of retyping the resource group's name and location into every child resource, reference them. A storage account reads azurerm_resource_group.orders.name and .location — so if the group changes, dependents follow automatically and stay consistent.
resource "azurerm_storage_account" "orders" {
name = "myorgordersdev"
resource_group_name = azurerm_resource_group.orders.name
location = azurerm_resource_group.orders.location
account_tier = "Standard"
account_replication_type = "LRS"
}
References are unquoted expressions
azurerm_resource_group.orders.name is an expression, not a string — write it bare, with no quotes. To embed it inside a larger string, use interpolation: "${azurerm_resource_group.orders.name}-data". Quoting the whole reference turns it into a literal that will never resolve.
Resolve a cross-resource reference
Model a resource group as { type: "azurerm_resource_group", name: "orders", attributes: { name, location } }. Write resolveRef(rg, path) where path is like "azurerm_resource_group.orders.location"; it returns the matching attribute value. Log resolveRef(rg, "azurerm_resource_group.orders.location") — it must print westus2.