Resource Addressing and Resource Parameters to Connect Resources

Learn about resource addressing and resource parameters that can be referenced within a configuration.

Resource addressing

The resources we create in our Terraform configuration will often be related. For instance, creating a web application that runs on virtual machines might involve a virtual network, virtual machines, network interfaces, firewall rules, and a load balancer. All these resources will refer to each other for their configuration. The load balancer will need to reference the network interfaces on the virtual machines, and the network interfaces will need to reference a subnet on the virtual network. Resource addressing is how we can reference the attributes of a resource within our Terraform configuration.

Format of resource addressing

We’ve already seen several examples of references to both named values and indexes. The general format is different depending on the object type in question. When referring to the value inside a variable, the format is var.<NAME>. Resources use the format resource_type.<NAME>, followed by the attribute with the value we’d like to retrieve. If we’ve used a for_each or count argument to create multiple instances of a resource, then the value returned will be a map or list, respectively.

When referencing a resource, we have access to any arguments we define in the configuration and any attributes that are exported by the resource. A simple example would be retrieving the name of an Azure resource group to be used by another resource.

Get hands-on with 1200+ tech skills courses.