WEBVTT

00:00.340 --> 00:04.100
-In this lecture,
we will understand the basics of HCL,

00:04.340 --> 00:07.060
which is HashiCorp Configuration Language,

00:07.540 --> 00:10.540
and then create a resource using Terraform.

00:12.180 --> 00:15.180
Let us first understand the HCL syntax.

00:15.720 --> 00:19.320
The HCL file consists of Blocks and Arguments.

00:19.980 --> 00:22.700
A block is defined within curly braces,

00:22.900 --> 00:26.920
and it contains a set of arguments
in key value pair format

00:27.060 --> 00:29.140
representing the configuration data.

00:30.160 --> 00:34.020
But what is a block
and what arguments does it contain?

00:35.060 --> 00:37.680
In its simplest form, a block in Terraform

00:37.760 --> 00:40.380
contains information
about the infrastructure platform

00:40.700 --> 00:44.440
and a set of resources within that platform
that we want to create.

00:45.320 --> 00:48.500
For example, let us consider a simple task.

00:49.160 --> 00:53.220
We want to create a file in the local system
where Terraform is installed.

00:54.660 --> 00:58.580
To do this, first,
let us create a directory called

00:58.720 --> 01:02.960
terraform-local-file under /root directory.

01:03.800 --> 01:08.140
This is the directory under which
we will create the HCL configuration file.

01:09.020 --> 01:11.360
Once we change into this new directory,

01:11.620 --> 01:16.000
we can create a configuration file
called local.tf.

01:17.000 --> 01:21.320
And within this file,
we can define a resource block, like this.

01:23.780 --> 01:25.280
And inside the Resource block,

01:25.500 --> 01:29.540
we specify the file name to be created
as well as its contents

01:29.640 --> 01:31.680
using the block arguments.

01:33.640 --> 01:38.520
Let us break down the local.tf file
to understand what each line means.

01:40.060 --> 01:42.660
The first element in this file is a block.

01:43.560 --> 01:46.840
Now this can be identified
by the curly braces inside.

01:47.960 --> 01:51.680
The type of block we see here
is called the "Resource" block,

01:52.380 --> 01:55.920
and this can be identified
by the keyword called "Resource"

01:55.980 --> 01:57.220
in the beginning of the block.

01:58.200 --> 02:00.060
Following the keyword called resource,

02:00.260 --> 02:03.880
we have the declaration of the resource type
that we want to create.

02:04.680 --> 02:07.880
This is a fixed value
and depends on the provider

02:07.960 --> 02:09.580
where we want to create the resource.

02:10.360 --> 02:14.000
In this case,
we have the resource type called "local_file."

02:16.140 --> 02:18.960
A resource type provides
two bits of information.

02:19.460 --> 02:21.160
First is the Provider,

02:21.780 --> 02:25.680
which is represented by the word
before the underscore in the resource type.

02:26.760 --> 02:29.760
Here we are making use of the "Local" provider.

02:30.440 --> 02:34.420
The word following the underscore,
which is "File" in this case,

02:34.620 --> 02:36.560
represents the type of resource.

02:38.580 --> 02:43.400
The next and final declaration
in this resource block is the resource name.

02:44.180 --> 02:47.640
This is the logical name
used to identify the resource,

02:47.900 --> 02:49.800
and it can be named anything.

02:50.560 --> 02:52.620
But in this case, we have called it pet,

02:53.000 --> 02:56.460
as the file we are creating
contains information about pets.

02:57.460 --> 03:00.660
And within this block
and inside the curly braces,

03:00.900 --> 03:06.280
we define the arguments for resource
which are written in key value pair format.

03:07.480 --> 03:11.240
These arguments are specific
to the type of resource we are creating,

03:11.340 --> 03:13.580
which in this case is the local_file.

03:14.800 --> 03:17.100
The first argument is the filename.

03:17.940 --> 03:22.060
To this, we assign the absolute path
to the file we want to create.

03:22.660 --> 03:27.400
In this example, it is set to /root/pets.txt.

03:28.660 --> 03:33.540
Now we can also add some content to this file
by making use of the content argument.

03:34.220 --> 03:38.260
To this, let us add the value "We love pets."

03:39.300 --> 03:41.580
The words filename and content

03:41.820 --> 03:44.900
are specific to the local_file resource
we want to create,

03:45.260 --> 03:46.840
and they cannot be changed.

03:47.760 --> 03:51.280
In other words,
the resource type of local_file

03:51.480 --> 03:55.380
expects that we provide the argument
of filename and content.

03:56.420 --> 03:59.760
Each resource type
has specific arguments that they expect.

04:00.580 --> 04:03.280
We will see more of that
as we progress through the course.

04:03.980 --> 04:07.880
And that's it,
we now have a complete HCL configuration file

04:08.140 --> 04:12.000
that we can use to create a file
by the name of "pets.txt."

04:12.780 --> 04:18.040
This file will be created in the /root directory,
and it'll contain a single line of data.

04:19.140 --> 04:20.640
The resource block that we see here

04:20.700 --> 04:24.400
is just one example
of the configuration blocks used in HCL,

04:24.820 --> 04:29.180
but it is also a mandatory block
needed to deploy a resource using Terraform.

04:30.340 --> 04:36.040
Here is an example of a resource file
created for provisioning an AWS ec2 instance.

04:36.740 --> 04:40.200
The resource type is aws_instance.

04:41.220 --> 04:43.220
We name the resource webserver

04:43.540 --> 04:48.960
and the arguments that we have used here
is the AMI ID and the instance type.

04:50.400 --> 04:55.500
Here is another example of a resource file
used to create an AWS S3 bucket.

04:56.240 --> 05:00.640
The resource type in this case is aws_s3_bucket.

05:01.180 --> 05:03.620
The resource name that we have chosen is data

05:03.960 --> 05:08.200
and the arguments that we have provided
is the bucket name and the acl.

05:09.820 --> 05:13.260
A simple Terraform workflow
consists of four steps.

05:14.320 --> 05:16.620
First, write the configuration file.

05:17.860 --> 05:20.680
Next, run the Terraform Init command.

05:22.100 --> 05:27.280
And after that, review the execution plan
using the terraform plan command.

05:28.620 --> 05:34.100
Finally, once we are ready, apply the changes
using the Terraform Apply command.

05:36.100 --> 05:38.020
With the configuration file ready,

05:38.140 --> 05:42.600
we can now create the file resource
using the terraform commands as follows,

05:43.700 --> 05:46.140
first, run the terraform init command.

05:46.820 --> 05:49.260
This command
will check the configuration file

05:49.440 --> 05:53.040
and initialize the working directory
containing the .TF file.

05:54.460 --> 05:56.760
One of the first things that this command does

05:56.900 --> 06:00.240
is to understand that we are making use
of the Local provider

06:00.400 --> 06:03.120
based on the resource type
declared in the resource block.

06:04.120 --> 06:05.720
It will then download the plugin

06:05.840 --> 06:09.780
to be able to work on the resources
declared in the .TF file.

06:11.600 --> 06:13.220
From the output of this command,

06:13.540 --> 06:17.840
we can see that terraform init
has installed a plugin called local.

06:19.520 --> 06:23.840
Next, we are ready to create the resource,
but before we do that,

06:24.080 --> 06:27.660
if we want to see the execution plan
that will be carried out by Terraform,

06:27.960 --> 06:30.700
we can use the command terraform plan.

06:31.760 --> 06:35.280
This command will show the actions
that will be carried out by Terraform

06:35.400 --> 06:36.700
to create the resource.

06:37.800 --> 06:40.220
Terraform knows
that it has to create resources,

06:40.580 --> 06:45.220
and this is displayed in the output
similar to a diff command in GIT.

06:46.040 --> 06:50.700
The output has a + symbol
next to the local_file type resource called pet.

06:51.720 --> 06:54.460
This includes all the arguments
that we specified

06:54.520 --> 06:56.880
in the .TF file for creating the resource.

06:57.360 --> 07:01.100
But you'll also notice that some default
or optional arguments

07:01.220 --> 07:04.220
which we did not specifically declare
in the configuration file

07:04.500 --> 07:06.400
is also displayed on the screen.

07:07.700 --> 07:10.840
The plus symbol
implies that the resource will be created.

07:11.900 --> 07:16.220
Now remember, this step will not create
the infrastructure resource yet.

07:17.400 --> 07:19.860
This information is provided for the user

07:19.960 --> 07:22.520
to review and ensure that all the actions

07:22.580 --> 07:25.320
to be performed
in this execution plan is desired.

07:26.640 --> 07:29.380
After the review, we can create the resource.

07:30.660 --> 07:34.400
And to do this, we will make use
of the Terraform Apply command.

07:36.040 --> 07:39.260
This command will display
the execution plan once again,

07:39.700 --> 07:44.160
and it will then ask the user to confirm
by typing Yes to proceed.

07:45.160 --> 07:48.620
Once we confirm, it will proceed
with the creation of the resource,

07:48.780 --> 07:50.540
which in this case is a file.

07:51.920 --> 07:54.880
We can validate that the file
was indeed created

07:55.000 --> 07:57.720
by running the cat command to view the file.

07:59.440 --> 08:02.000
-We can also run the "terraform show command"

08:02.100 --> 08:03.760
within the configuration directory

08:03.840 --> 08:06.780
to see the details of the resource
that we just created.

08:07.740 --> 08:11.500
This command inspects the state file
and displays the resource details.

08:11.980 --> 08:15.580
We will learn more about this command
and the state in a later lecture.

08:17.500 --> 08:20.940
-So, we have now created
our first resource using Terraform.

08:21.640 --> 08:23.180
Before we end this section,

08:23.420 --> 08:27.900
let us go back and look at
the configuration blocks in local.tf file.

08:29.460 --> 08:33.180
In this example,
we used the resource type of local_file

08:33.420 --> 08:36.320
and learnt that the keyword
before the underscore here

08:36.460 --> 08:38.560
is the provider name called "local."

08:39.340 --> 08:41.040
But how do we know that?

08:41.700 --> 08:43.840
How do we know what resource types

08:43.900 --> 08:47.580
other than local_file are available
under the provider called local?

08:48.100 --> 08:53.260
And finally, how do we know what arguments
are expected by the local_file resource?

08:55.160 --> 08:59.440
Earlier, we mentioned
that Terraform supports over 100 providers,

08:59.640 --> 09:02.760
including the local provider
we have used in this example.

09:03.640 --> 09:08.940
Other common examples are AWS
to deploy resources in Amazon AWS cloud,

09:09.920 --> 09:13.400
Azure, GCP, Ali Cloud, et cetera.

09:14.740 --> 09:17.580
Each of these providers
have a unique list of resources

09:17.660 --> 09:20.480
that can be created
on that specific platform.

09:21.480 --> 09:26.140
And each resource can have a number of required
or optional arguments

09:26.220 --> 09:28.120
that are needed to create that resource

09:29.540 --> 09:33.320
And we can create
as many resources of each type as needed.

09:35.100 --> 09:38.020
It is impossible to remember
all of these options,

09:38.240 --> 09:40.400
and of course, we don't have to do that.

09:41.580 --> 09:44.460
Terraform documentation
is extremely comprehensive,

09:44.640 --> 09:47.800
and it is the single source of truth
that we need to follow.

09:48.920 --> 09:52.180
If we look up the local provider
within the documentation,

09:52.480 --> 09:56.960
we can see that it only has one type of resource
called the local_file.

09:58.120 --> 09:59.600
Under the arguments section,

09:59.860 --> 10:03.520
we can see that there are several arguments
that the resource block accepts,

10:03.780 --> 10:07.200
out of which only one is mandatory,
the file name.

10:07.860 --> 10:10.020
The rest of the arguments are optional.

10:11.240 --> 10:12.480
That's it for this lecture.

10:12.880 --> 10:17.500
Now let's head over to the hands on labs
and practice working with HCL

10:17.780 --> 10:20.660
and create our first resource
using Terraform.

