WEBVTT

00:01.530 --> 00:04.960
In this lecture, we will learn how
to link two resources together

00:05.040 --> 00:07.770
by making use
of resource attributes.

00:09.470 --> 00:12.330
In the last few lectures,
we saw how to use variables

00:12.410 --> 00:14.840
to improve the
reusability of our code.

00:15.750 --> 00:18.700
Right now, we have two resources
in our configuration file.

00:19.420 --> 00:22.440
Each of this resource has a set
of arguments that are used

00:22.520 --> 00:23.900
to create that resource.

00:24.920 --> 00:25.780
For the file resource,

00:25.860 --> 00:28.570
we have used the filename
and content as the arguments.

00:29.530 --> 00:31.920
For the pet resource,
we have used the prefix,

00:32.000 --> 00:33.440
separator and length.

00:34.870 --> 00:36.490
When this configuration
is applied,

00:36.570 --> 00:39.710
Terraform creates a file
and a random_pet resource.

00:40.640 --> 00:44.560
The name of the random_pet is
displayed on the screen as an ID,

00:44.640 --> 00:46.620
which is Mr. Bull in this case.

00:48.610 --> 00:52.010
As it stands, there is no dependency
between these two resources,

00:52.720 --> 00:54.240
but this rarely ever happens

00:54.320 --> 00:58.040
in a real world infrastructure
provisioning process.

00:58.120 --> 01:00.580
There are bound to be multiple
resources that are dependent

01:00.800 --> 01:01.640
on each other.

01:03.430 --> 01:06.850
What if we want to make use of
the output of one resource

01:06.930 --> 01:09.070
and use it as an input
for another one?

01:10.600 --> 01:13.560
What if we want the content
of the file to use the name

01:13.640 --> 01:15.900
generated by the
random_pet resource?

01:17.340 --> 01:19.500
Currently, the content
of the file is set to

01:19.840 --> 01:21.580
"My favorite pet is Mr. Cat".

01:23.160 --> 01:26.470
What if we want it to be set to
the name that is generated

01:26.550 --> 01:28.110
by the random_pet resource?

01:29.650 --> 01:32.310
To understand this, let us jump
back to the documentation

01:32.390 --> 01:36.350
for the random_pet resource
in registry.terraform.io.

01:37.460 --> 01:39.650
You would have noticed that
there are plenty of examples

01:39.730 --> 01:42.450
which are provided
in the documentation,

01:42.530 --> 01:44.420
including examples
for the arguments,

01:44.500 --> 01:47.730
but there is also a section
called attribute reference,

01:47.810 --> 01:50.630
which provides the list of
attributes returned back

01:50.710 --> 01:53.290
from the resource after you
run a Terraform apply.

01:55.010 --> 01:58.100
In this case, the random_pet
resource returns just one attribute

01:58.180 --> 02:00.510
called ID, which is of type string.

02:01.590 --> 02:05.280
The ID is the pet name generated
after we run Terraform apply

02:05.360 --> 02:06.460
as we have seen before.

02:08.490 --> 02:12.980
Our goal is to make use of the
attribute called ID and to make use

02:13.060 --> 02:15.960
of it as the content
of the local file resource.

02:17.040 --> 02:20.520
For this, we can make use
of an expression like this.

02:21.840 --> 02:24.430
This expression is used to
reference the attribute

02:24.510 --> 02:26.250
from the resource called my-pet.

02:27.080 --> 02:30.770
The syntax for using this
reference expression is resource type

02:30.850 --> 02:34.270
followed by the resource
name and the attribute to be used,

02:34.350 --> 02:36.940
all of which are separated
by a period or a dot.

02:38.160 --> 02:41.020
The resource type in this
example is random_pet.

02:41.890 --> 02:45.280
This is followed by my-pet,
which is the resource name

02:45.900 --> 02:47.630
and the attribute used is ID.

02:48.620 --> 02:52.090
You would have also noticed that
we are making use of the dollar symbol

02:52.170 --> 02:53.490
followed by the expression

02:53.570 --> 02:55.600
which is enclosed
within curly braces.

02:56.410 --> 02:58.830
This is known as an
interpolation sequence.

02:59.980 --> 03:03.690
Since the content argument
already uses a string type data,

03:04.150 --> 03:06.840
this sequence is used to
evaluate the expression given

03:06.920 --> 03:10.240
between the curly braces,
convert the result to a string,

03:10.320 --> 03:13.230
and then insert it to the
final string, like this.

03:14.490 --> 03:17.520
Now, let us apply the changes
made and allow Terraform

03:17.600 --> 03:19.150
to recreate the local file.

03:20.030 --> 03:23.090
In the output, you can see that
the content is being replaced

03:23.170 --> 03:26.490
to our desired value, and it
contains the name of the pet

03:26.570 --> 03:29.260
that was generated by
the random_pet resource.

03:30.340 --> 03:31.630
That's it for this lecture.

03:31.710 --> 03:34.000
Now, let's head over
to the hands-on labs

03:34.080 --> 03:36.540
and explore working with
reference expressions

03:36.620 --> 03:38.520
and resource attributes in Terraform.

