In this lecture, we will learn how to link two resources together
by making use of resource attributes.
In the last few lectures, we saw how to use variables
to improve the reusability of our code.
Right now, we have two resources in our configuration file.
Each of this resource has a set of arguments that are used
to create that resource.
For the file resource,
we have used the filename and content as the arguments.
For the pet resource, we have used the prefix,
separator and length.
When this configuration is applied,
Terraform creates a file and a random_pet resource.
The name of the random_pet is displayed on the screen as an ID,
which is Mr. Bull in this case.
As it stands, there is no dependency between these two resources,
but this rarely ever happens
in a real world infrastructure provisioning process.
There are bound to be multiple resources that are dependent
on each other.
What if we want to make use of the output of one resource
and use it as an input for another one?
What if we want the content of the file to use the name
generated by the random_pet resource?
Currently, the content of the file is set to
"My favorite pet is Mr. Cat".
What if we want it to be set to the name that is generated
by the random_pet resource?
To understand this, let us jump back to the documentation
for the random_pet resource in registry.terraform.io.
You would have noticed that there are plenty of examples
which are provided in the documentation,
including examples for the arguments,
but there is also a section called attribute reference,
which provides the list of attributes returned back
from the resource after you run a Terraform apply.
In this case, the random_pet resource returns just one attribute
called ID, which is of type string.
The ID is the pet name generated after we run Terraform apply
as we have seen before.
Our goal is to make use of the attribute called ID and to make use
of it as the content of the local file resource.
For this, we can make use of an expression like this.
This expression is used to reference the attribute
from the resource called my-pet.
The syntax for using this reference expression is resource type
followed by the resource name and the attribute to be used,
all of which are separated by a period or a dot.
The resource type in this example is random_pet.
This is followed by my-pet, which is the resource name
and the attribute used is ID.
You would have also noticed that we are making use of the dollar symbol
followed by the expression
which is enclosed within curly braces.
This is known as an interpolation sequence.
Since the content argument already uses a string type data,
this sequence is used to evaluate the expression given
between the curly braces, convert the result to a string,
and then insert it to the final string, like this.
Now, let us apply the changes made and allow Terraform
to recreate the local file.
In the output, you can see that the content is being replaced
to our desired value, and it contains the name of the pet
that was generated by the random_pet resource.
That's it for this lecture.
Now, let's head over to the hands-on labs
and explore working with reference expressions
and resource attributes in Terraform.