Terraform Basics Training Course

Terraform Basics

Resource Attributes

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 Interpolation Syntax

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:

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:

random_pet.my-pet: Creating...
local_file.pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mr.bull]
local_file.pet: Creation complete after 0s [id=059090e865890f9b6debfd7aebf48fdce2220a6]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

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:

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:

random_pet.my-pet: Creating...
local_file.pet: Creating...
random_pet.my-pet: Creation complete after 0s [id=Mr.bull]
local_file.pet: Creation complete after 0s [id=059090e865809f9b6debfd7aebf48fdce2220a6]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

In this configuration:

  • The resource type is "random_pet"
  • The resource name is "my-pet"
  • The referenced attribute is "id"

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:

$ terraform apply
.
.
# local_file.pet must be replaced
resource "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 0s
local_file.pet: Creating...
local_file.pet: Creation complete after 0s
  [id=e56101d304de7cf1b1001120923c6bdeaaa60c523]
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Further Exploration

Explore working with resource taints and various resource attributes in Terraform to further optimize your infrastructure management.

For more detailed information, refer to the Terraform Documentation and Kubernetes Basics.

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Using Variables in Terraform