In this section,
we will take a look at
data sources in Terraform.
We know by now that
Terraform makes use of configuration files
along with the state file
to provision infrastructure resources,
but as we saw earlier,
Terraform is just one of the infrastructure
as a code tool
that can be used for provisioning.
Infrastructure can be
provisioned using other tools, such as
Puppet CloudFormation,
SaltStack,
Ansible, et cetera.
Not to mention ad-hoc scripts
and manually provisioned infrastructure
or even resources that are created
by Terraform from another configuration directory.
For instance,
let us assume that a database instance
was provisioned manually in the AWS cloud.
Although Terraform does not
manage this resource,
it can read attributes
such as the database name,
host address,
or the DB user,
and use it to provision an application resource
that is managed by Terraform.
Let's take a simpler example.
We have a local file resource called
"Pet" created with the contents
"We love pets."
Once this resource is provisioned,
the file is created in the /root directory,
and the information about this file
is also stored in the Terraform state file.
Now,
let's create a new file using a simple
shell script like this.
Quite evidently,
this file is outside the control
and management or Terraform at this point in time.
The local file resource that Terraform
is in charge of
is Petstore TXT
under the /root directory
and has no relationship with the local
file called dogs.TXT,
which is also created under the /root directory.
The dogs.TXT has
a single line
that says, "Dogs are awesome."
We would like Terraform to use this file
as a data source
and use its data as contents of
our existing file called Petstore TXT.
If you want to make use of the attributes
of this new file
that is created by the bar script,
we can make use of data sources.
Data sources allow Terraform
to read attributes from resources
which are provisioned outside its control.
For example,
to read the attributes from the local file
called dogs.TXT,
we can define a data block within
the configuration file, like this.
As you may have noticed,
the data block is
quite similar to the resource block.
Instead of the keyword called resource,
we define a data source block
with the keyword called "data."
This is followed by the type of
resource which we are trying to read.
In this example,
it's a local file.
This can be any valid resource type
for any provider supported by Terraform.
Next comes a logical resource name
into which the attributes
for a resource will be read.
Within the block, we have arguments
just like we have in a normal resource block.
The data source block
consists of specific arguments for a data source,
and to know which argument is expected,
we can look up the provider documentation
in Terraform registry.
For the local file data source,
we just have one argument that should be used,
which is the file name to be read.
The data read from a data source
is then available under the
"data object" in Terraform.
So,
To use this data
in the resource called "Pet",
we could simply use
data.local_file.dog.content.
These details are of course,
available in the Terraform documentation,
under "Data sources."
Within the documentation
and under the attributes exported,
we can see that the data source
for a local file expose two attributes,
the content
and the B64 encoded version of the content.
To distinguish between a resource
and data sources,
let's do a quick comparison.
The resources are created with the resource block,
and data sources are created with the
data block.
Resources in Terraform are used to create,
update and destroy infrastructure,
whereas a data source
is used to read information from a specific resource.
Regular resources are also called
"managed resources"
as it's an extension which is managed by Terraform.
Data sources are also called data resources.
That's it for this lecture.
Now let's head over to the hands-on labs
and practice working with data sources in Terraform.