WEBVTT

00:00.540 --> 00:01.680
In this section,

00:01.680 --> 00:03.000
we will take a look at

00:03.000 --> 00:05.000
data sources in Terraform.

00:06.510 --> 00:07.800
We know by now that

00:07.800 --> 00:10.340
Terraform makes use of configuration files

00:10.340 --> 00:11.620
along with the state file

00:11.620 --> 00:13.880
to provision infrastructure resources,

00:14.620 --> 00:16.020
but as we saw earlier,

00:16.020 --> 00:17.910
Terraform is just one of the infrastructure

00:17.910 --> 00:18.940
as a code tool

00:18.940 --> 00:20.940
that can be used for provisioning.

00:21.600 --> 00:22.680
Infrastructure can be

00:22.680 --> 00:24.680
provisioned using other tools, such as

00:24.680 --> 00:26.450
Puppet CloudFormation,

00:26.450 --> 00:27.400
SaltStack,

00:27.400 --> 00:29.400
Ansible, et cetera.

00:29.400 --> 00:31.200
Not to mention ad-hoc scripts

00:31.200 --> 00:33.200
and manually provisioned infrastructure

00:33.310 --> 00:35.250
or even resources that are created

00:35.250 --> 00:37.680
by Terraform from another configuration directory.

00:38.620 --> 00:39.510
For instance,

00:39.650 --> 00:41.370
let us assume that a database instance

00:41.370 --> 00:43.970
was provisioned manually in the AWS cloud.

00:44.450 --> 00:45.970
Although Terraform does not

00:45.970 --> 00:46.910
manage this resource,

00:47.000 --> 00:48.310
it can read attributes

00:48.310 --> 00:50.310
such as the database name,

00:50.310 --> 00:51.170
host address,

00:51.170 --> 00:52.400
or the DB user,

00:52.400 --> 00:54.770
and use it to provision an application resource

00:54.770 --> 00:56.770
that is managed by Terraform.

00:57.250 --> 00:58.910
Let's take a simpler example.

00:59.220 --> 01:01.140
We have a local file resource called

01:01.140 --> 01:03.140
"Pet" created with the contents

01:03.140 --> 01:04.570
"We love pets."

01:04.880 --> 01:06.600
Once this resource is provisioned,

01:06.600 --> 01:09.220
the file is created in the /root directory,

01:09.480 --> 01:11.200
and the information about this file

01:11.200 --> 01:13.680
is also stored in the Terraform state file.

01:14.450 --> 01:15.000
Now,

01:15.000 --> 01:17.000
let's create a new file using a simple

01:17.000 --> 01:19.000
shell script like this.

01:19.480 --> 01:20.400
Quite evidently,

01:20.400 --> 01:22.400
this file is outside the control

01:22.400 --> 01:24.620
and management or Terraform at this point in time.

01:25.480 --> 01:27.170
The local file resource that Terraform

01:27.170 --> 01:28.370
is in charge of

01:28.370 --> 01:29.940
is Petstore TXT

01:29.940 --> 01:31.370
under the /root directory

01:31.570 --> 01:33.370
and has no relationship with the local

01:33.370 --> 01:35.370
file called dogs.TXT,

01:35.370 --> 01:38.250
which is also created under the /root directory.

01:38.880 --> 01:40.250
The dogs.TXT has

01:40.250 --> 01:41.310
a single line

01:41.310 --> 01:43.970
that says, "Dogs are awesome."

01:44.250 --> 01:46.250
We would like Terraform to use this file

01:46.250 --> 01:47.420
as a data source

01:47.420 --> 01:49.420
and use its data as contents of

01:49.420 --> 01:52.050
our existing file called Petstore TXT.

01:52.680 --> 01:54.510
If you want to make use of the attributes

01:54.510 --> 01:55.800
of this new file

01:55.800 --> 01:57.540
that is created by the bar script,

01:57.540 --> 01:59.540
we can make use of data sources.

02:00.880 --> 02:02.340
Data sources allow Terraform

02:02.340 --> 02:04.340
to read attributes from resources

02:04.340 --> 02:06.340
which are provisioned outside its control.

02:06.740 --> 02:07.710
For example,

02:07.710 --> 02:09.800
to read the attributes from the local file

02:09.800 --> 02:11.450
called dogs.TXT,

02:11.450 --> 02:13.450
we can define a data block within

02:13.450 --> 02:15.450
the configuration file, like this.

02:16.050 --> 02:17.250
As you may have noticed,

02:17.250 --> 02:18.370
the data block is

02:18.370 --> 02:20.370
quite similar to the resource block.

02:20.650 --> 02:22.800
Instead of the keyword called resource,

02:22.800 --> 02:24.400
we define a data source block

02:24.400 --> 02:26.400
with the keyword called "data."

02:26.770 --> 02:28.310
This is followed by the type of

02:28.310 --> 02:30.310
resource which we are trying to read.

02:30.480 --> 02:31.600
In this example,

02:31.600 --> 02:33.600
it's a local file.

02:33.970 --> 02:35.880
This can be any valid resource type

02:35.880 --> 02:38.770
for any provider supported by Terraform.

02:39.310 --> 02:41.400
Next comes a logical resource name

02:41.400 --> 02:42.800
into which the attributes

02:42.800 --> 02:44.650
for a resource will be read.

02:44.970 --> 02:47.000
Within the block, we have arguments

02:47.000 --> 02:49.620
just like we have in a normal resource block.

02:50.050 --> 02:51.110
The data source block

02:51.110 --> 02:53.770
consists of specific arguments for a data source,

02:54.000 --> 02:56.170
and to know which argument is expected,

02:56.170 --> 02:57.880
we can look up the provider documentation

02:57.880 --> 02:59.880
in Terraform registry.

03:00.080 --> 03:01.740
For the local file data source,

03:01.740 --> 03:03.940
we just have one argument that should be used,

03:03.940 --> 03:05.940
which is the file name to be read.

03:07.710 --> 03:09.600
The data read from a data source

03:09.600 --> 03:11.110
is then available under the

03:11.110 --> 03:13.110
"data object" in Terraform.

03:15.050 --> 03:15.680
So,

03:15.800 --> 03:17.170
To use this data

03:17.170 --> 03:18.510
in the resource called "Pet",

03:18.510 --> 03:19.680
we could simply use

03:19.740 --> 03:23.570
data.local_file.dog.content.

03:25.710 --> 03:27.170
These details are of course,

03:27.170 --> 03:29.170
available in the Terraform documentation,

03:29.170 --> 03:30.280
under "Data sources."

03:30.600 --> 03:31.880
Within the documentation

03:31.880 --> 03:33.880
and under the attributes exported,

03:33.940 --> 03:35.880
we can see that the data source

03:35.880 --> 03:37.880
for a local file expose two attributes,

03:38.000 --> 03:38.910
the content

03:38.910 --> 03:41.770
and the B64 encoded version of the content.

03:43.050 --> 03:44.740
To distinguish between a resource

03:44.740 --> 03:45.770
and data sources,

03:45.770 --> 03:47.770
let's do a quick comparison.

03:48.650 --> 03:51.170
The resources are created with the resource block,

03:51.170 --> 03:53.170
and data sources are created with the

03:53.170 --> 03:54.140
data block.

03:54.510 --> 03:56.850
Resources in Terraform are used to create,

03:56.850 --> 03:58.850
update and destroy infrastructure,

03:58.940 --> 04:00.310
whereas a data source

04:00.310 --> 04:03.310
is used to read information from a specific resource.

04:04.140 --> 04:06.080
Regular resources are also called

04:06.080 --> 04:07.280
"managed resources"

04:07.280 --> 04:09.880
as it's an extension which is managed by Terraform.

04:10.200 --> 04:13.770
Data sources are also called data resources.

04:15.400 --> 04:17.000
That's it for this lecture.

04:17.370 --> 04:19.650
Now let's head over to the hands-on labs

04:19.650 --> 04:22.650
and practice working with data sources in Terraform.

