Instructor: Until now, we have seen quite
a few Terraform commands in action,
such as the Terraform init, plan, and apply.
Let us now take a look at some more commands
available in Terraform.
The first command we will take a look at
is the terraform validate command.
Once we write our configuration file,
it's not necessary to run Terraform plan or apply
to check if the syntax used is correct.
Instead, we can make use of the Terraform validate command
like this, and if everything is correct with the file,
we should see a successful validation message like this.
If there's an error in the configuration file,
the validate command will show you the line in the file
that is causing the error with the hints to fix it.
In this example, we have used an incorrect argument
for the local_file resource.
It should be file_permission and not file_permissions.
The next command that we are going
to see is the terraform fmt or the Terraform format command.
This command scans the configuration files
in the current working directory and formats the code
into a canonical format.
This is a useful command to improve the readability
of the Terraform configuration file.
When we're on this command,
the files that are changed in the configuration directory
is displayed on the screen.
The terraform show command prints out the current state
of the infrastructure as seen by Terraform.
In this example,
we have already created the local_file resource
and when we run the show command,
it displays the current state of the resource,
including all the attributes created by Terraform
for that resource, such as the file name,
file and directory permissions, content,
and ID of the resource.
Additionally, we can make use of the -json flag
to print the contents in an JSON format.
To see a list of all providers used
in the configuration directory,
use the terraform providers command.
You can also make use of the mirror sub-command
to copy provider plugins needed
for the current configuration
to another directory like this.
This command will mirror the provider configuration
in a new path /rout/terraform/new_ local_file.
We saw how to use terraform output variables
in one of the previous lectures.
If you want to print all output variables
in the configuration directory,
use the command terraform output.
You can also print the value of a specific variable
by appending the name of the variable
to the end of the output command like this.
The Terraform refresh command is used to sync Terraform
with the real world infrastructure.
For example, if there are any changes made
to a resource created by Terraform outside its control,
such as a manual update,
the terraform refresh command will pick it up
and update the state file.
This reconciliation is useful to determine
what action to take during the next apply.
This command will not modify any infrastructure resource,
but it will modify the state file.
As we saw earlier,
terraform refresh is also run automatically by command,
such as terraform plan and terraform apply,
and this is done prior
to Terraform generating an execution plan.
This can however be bypassed
by using the -refresh is equal to false option
with the commands.
The terraform graph command is used
to create a visual representation
of the dependencies in a Terraform configuration
or an execution plan.
In this example,
the local_file in our main.tf file has a dependency
on the random_pet resource.
This command can be run as soon
as you have the configuration file ready,
even before you have initial configuration directory
with Terraform in it.
Upon running the terraform graph command,
you should see an output like this.
This text generated is hard to comprehend as it is,
but it is a graph generated in a format called DOT.
To make more sense of this graph,
we can pass it through a graph visualization software,
such as Graphviz
and we can install it in Ubuntu using apt like this.
Once installed, we can pass the output
of the Terraform graph to the dot command,
which we installed using the Graphviz package,
and generate a graphic like this.
We can now open this file via a browser
and it should show a dependency graph like this.
The root is the configuration directory
where the configuration for this graph is located.
We can see that there are two resources,
the local_file called pet
and the random_pet resource called my.pet
that makes use of the local
and the random provider respectively.
And finally, we can see that the local_file called pet
depends on the random_pet resource called my.pet
as we have used the reference expression
and the local_file resource
that points to the ID of the random_pet.
That's it for this lecture.
Now, let's head over to the hands-on labs
and explore working with the Terraform commands
that we learned in this lecture.