Learn to link resources in Terraform using attributes, enhancing infrastructure interdependence and dynamic configurations.
In this lesson, you’ll learn how to link two resources together using resource attributes in Terraform. Building on previous concepts of variable reuse, we now extend the idea by connecting two resources, making your infrastructure more dynamic and interdependent.Terraform configurations often contain multiple resources, each defined with its specific arguments. For example, our configuration includes a file resource and a random pet resource. For the file resource, we specify the filename and its content. The pet resource, on the other hand, sets parameters like prefix, separator, and length. When applied, Terraform creates both resources, and the random pet’s generated name appears as an ID (e.g., “Mr.bull”).In real-world infrastructure scenarios, resources frequently depend on each other. Consider a scenario where you want to use the output of one resource as an input for another. In our case, we want the content of the file to include the pet name generated by the random pet resource, rather than a hardcoded string like “My favorite pet is Mr.Cat.”
Terraform’s interpolation syntax allows you to reference another resource’s attribute using the format: resource_type.resource_name.attribute. This ensures that when one resource’s output changes, the dependent resource will update accordingly.
Below is the initial configuration file:
Copy
Ask AI
resource "local_file" "pet" { filename = var.filename content = "My favorite pet is Mr.Cat"}resource "random_pet" "my-pet" { prefix = var.prefix separator = var.separator length = var.length}
When you run terraform apply, the output might look like this:
The Terraform Registry documentation on registry.terraform.io provides plenty of examples for resource arguments and displays the attributes returned after resource creation. In our scenario, the random pet resource returns an attribute, “id”, representing the generated pet name.Our goal is to capture this “id” attribute and utilize it as the file content. Here’s the updated configuration using Terraform’s interpolation syntax:
Copy
Ask AI
resource "local_file" "pet" { filename = var.filename content = "My favorite pet is ${random_pet.my-pet.id}"}resource "random_pet" "my-pet" { prefix = var.prefix separator = var.separator length = var.length}
After running terraform apply with this updated configuration, you should see output similar to:
The interpolation sequence (${...}) converts the evaluated expression into a string and dynamically inserts the pet name into the file content.Now, let’s apply the changes so that Terraform updates the local file. The output will indicate that the file content has been updated with the new pet name:
Copy
Ask AI
$ terraform apply..# local_file.pet must be replacedresource "local_file" "pet" { content = "My favorite pet is Mrs.Cat!" -> "My favorite pet is Mr.bull" # forces replacement directory_permission = "0777" file_permission = "0777" filename = "/roots/pets.txt" id = "98af5244e23508cffd4a0c3c46546821c4ccbbd0" -> (known after apply)}..local_file.pet: Destroying... [id=98af5244e23508cffd4a0c3c46546821c4ccbbd0]local_file.pet: Destruction complete after 0slocal_file.pet: Creating...local_file.pet: Creation complete after 0s [id=e56101d304de7cf1b1001120923c6bdeaaa60c523]Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
Explore working with resource taints and various resource attributes in Terraform to further optimize your infrastructure management.