WEBVTT

00:00.540 --> 00:02.280
Instructor: Until now, we have seen quite

00:02.280 --> 00:04.710
a few Terraform commands in action,

00:04.710 --> 00:08.193
such as the Terraform init, plan, and apply.

00:09.090 --> 00:11.460
Let us now take a look at some more commands

00:11.460 --> 00:13.167
available in Terraform.

00:14.040 --> 00:16.320
The first command we will take a look at

00:16.320 --> 00:18.423
is the terraform validate command.

00:19.560 --> 00:21.930
Once we write our configuration file,

00:21.930 --> 00:25.110
it's not necessary to run Terraform plan or apply

00:25.110 --> 00:28.440
to check if the syntax used is correct.

00:28.440 --> 00:31.740
Instead, we can make use of the Terraform validate command

00:31.740 --> 00:35.610
like this, and if everything is correct with the file,

00:35.610 --> 00:39.123
we should see a successful validation message like this.

00:40.110 --> 00:42.750
If there's an error in the configuration file,

00:42.750 --> 00:45.510
the validate command will show you the line in the file

00:45.510 --> 00:48.513
that is causing the error with the hints to fix it.

00:49.440 --> 00:52.890
In this example, we have used an incorrect argument

00:52.890 --> 00:55.050
for the local_file resource.

00:55.050 --> 00:58.623
It should be file_permission and not file_permissions.

00:59.730 --> 01:01.050
The next command that we are going

01:01.050 --> 01:04.923
to see is the terraform fmt or the Terraform format command.

01:06.060 --> 01:08.190
This command scans the configuration files

01:08.190 --> 01:11.370
in the current working directory and formats the code

01:11.370 --> 01:13.173
into a canonical format.

01:14.040 --> 01:16.530
This is a useful command to improve the readability

01:16.530 --> 01:18.513
of the Terraform configuration file.

01:19.620 --> 01:21.360
When we're on this command,

01:21.360 --> 01:24.090
the files that are changed in the configuration directory

01:24.090 --> 01:25.773
is displayed on the screen.

01:27.240 --> 01:30.150
The terraform show command prints out the current state

01:30.150 --> 01:32.613
of the infrastructure as seen by Terraform.

01:33.450 --> 01:34.860
In this example,

01:34.860 --> 01:38.040
we have already created the local_file resource

01:38.040 --> 01:40.590
and when we run the show command,

01:40.590 --> 01:42.960
it displays the current state of the resource,

01:42.960 --> 01:45.690
including all the attributes created by Terraform

01:45.690 --> 01:48.510
for that resource, such as the file name,

01:48.510 --> 01:51.180
file and directory permissions, content,

01:51.180 --> 01:52.593
and ID of the resource.

01:53.490 --> 01:57.000
Additionally, we can make use of the -json flag

01:57.000 --> 01:59.493
to print the contents in an JSON format.

02:00.990 --> 02:03.150
To see a list of all providers used

02:03.150 --> 02:04.950
in the configuration directory,

02:04.950 --> 02:07.203
use the terraform providers command.

02:08.190 --> 02:10.890
You can also make use of the mirror sub-command

02:10.890 --> 02:12.810
to copy provider plugins needed

02:12.810 --> 02:14.670
for the current configuration

02:14.670 --> 02:17.610
to another directory like this.

02:17.610 --> 02:20.040
This command will mirror the provider configuration

02:20.040 --> 02:24.723
in a new path /rout/terraform/new_ local_file.

02:27.660 --> 02:30.090
We saw how to use terraform output variables

02:30.090 --> 02:32.610
in one of the previous lectures.

02:32.610 --> 02:34.710
If you want to print all output variables

02:34.710 --> 02:36.570
in the configuration directory,

02:36.570 --> 02:39.930
use the command terraform output.

02:39.930 --> 02:43.200
You can also print the value of a specific variable

02:43.200 --> 02:45.060
by appending the name of the variable

02:45.060 --> 02:48.123
to the end of the output command like this.

02:49.680 --> 02:52.620
The Terraform refresh command is used to sync Terraform

02:52.620 --> 02:55.470
with the real world infrastructure.

02:55.470 --> 02:58.170
For example, if there are any changes made

02:58.170 --> 03:01.740
to a resource created by Terraform outside its control,

03:01.740 --> 03:03.825
such as a manual update,

03:03.825 --> 03:05.850
the terraform refresh command will pick it up

03:05.850 --> 03:07.353
and update the state file.

03:08.190 --> 03:10.830
This reconciliation is useful to determine

03:10.830 --> 03:14.133
what action to take during the next apply.

03:15.330 --> 03:18.360
This command will not modify any infrastructure resource,

03:18.360 --> 03:20.403
but it will modify the state file.

03:21.930 --> 03:23.400
As we saw earlier,

03:23.400 --> 03:26.490
terraform refresh is also run automatically by command,

03:26.490 --> 03:29.163
such as terraform plan and terraform apply,

03:30.000 --> 03:31.110
and this is done prior

03:31.110 --> 03:34.173
to Terraform generating an execution plan.

03:35.190 --> 03:36.870
This can however be bypassed

03:36.870 --> 03:39.480
by using the -refresh is equal to false option

03:39.480 --> 03:40.443
with the commands.

03:41.790 --> 03:43.740
The terraform graph command is used

03:43.740 --> 03:45.510
to create a visual representation

03:45.510 --> 03:48.300
of the dependencies in a Terraform configuration

03:48.300 --> 03:50.073
or an execution plan.

03:50.910 --> 03:52.170
In this example,

03:52.170 --> 03:55.950
the local_file in our main.tf file has a dependency

03:55.950 --> 03:58.530
on the random_pet resource.

03:58.530 --> 04:00.210
This command can be run as soon

04:00.210 --> 04:02.220
as you have the configuration file ready,

04:02.220 --> 04:05.070
even before you have initial configuration directory

04:05.070 --> 04:06.453
with Terraform in it.

04:07.500 --> 04:09.690
Upon running the terraform graph command,

04:09.690 --> 04:12.300
you should see an output like this.

04:12.300 --> 04:15.660
This text generated is hard to comprehend as it is,

04:15.660 --> 04:19.263
but it is a graph generated in a format called DOT.

04:20.700 --> 04:22.470
To make more sense of this graph,

04:22.470 --> 04:25.320
we can pass it through a graph visualization software,

04:25.320 --> 04:27.390
such as Graphviz

04:27.390 --> 04:31.053
and we can install it in Ubuntu using apt like this.

04:32.220 --> 04:34.530
Once installed, we can pass the output

04:34.530 --> 04:36.810
of the Terraform graph to the dot command,

04:36.810 --> 04:39.990
which we installed using the Graphviz package,

04:39.990 --> 04:42.003
and generate a graphic like this.

04:43.050 --> 04:45.810
We can now open this file via a browser

04:45.810 --> 04:48.483
and it should show a dependency graph like this.

04:49.650 --> 04:51.840
The root is the configuration directory

04:51.840 --> 04:54.340
where the configuration for this graph is located.

04:55.440 --> 04:57.810
We can see that there are two resources,

04:57.810 --> 04:59.760
the local_file called pet

04:59.760 --> 05:02.370
and the random_pet resource called my.pet

05:02.370 --> 05:03.780
that makes use of the local

05:03.780 --> 05:06.630
and the random provider respectively.

05:06.630 --> 05:09.690
And finally, we can see that the local_file called pet

05:09.690 --> 05:12.750
depends on the random_pet resource called my.pet

05:12.750 --> 05:14.580
as we have used the reference expression

05:14.580 --> 05:16.110
and the local_file resource

05:16.110 --> 05:18.393
that points to the ID of the random_pet.

05:19.410 --> 05:20.760
That's it for this lecture.

05:20.760 --> 05:22.920
Now, let's head over to the hands-on labs

05:22.920 --> 05:25.080
and explore working with the Terraform commands

05:25.080 --> 05:26.680
that we learned in this lecture.
