terraform console – Infrastructure as Code (IaC) with Terraform

The terraform console command provides an interactive console to investigate state files, dynamically build paths, and evaluate expressions even before using them in resources. It is a potent tool that most advanced Terraform users use. For example, let’s launch the console and look through the configuration of the VM resource we just imported.

Use the following commands to launch the console and get the resource group of the VM and the id value:
$ terraform console
Acquiring state lock. This may take a few moments…
azurerm_virtual_machine.main.resource_group_name “terraform-ws-dev”
azurerm_virtual_machine.main.id
“/subscriptions/id/resourceGroups/terraform-ws-dev/providers/Microsoft.Compute/
virtualMachines/httpd”

exit
Releasing state lock. This may take a few moments…

As we can see, the VM is in the correct resource group, and we’re satisfied that the import was correct.

Terraform dependencies and graphs

Terraform uses a dependency model to manage in what order resources are created and destroyed. There are two kinds of dependencies – implicit and explicit. We’ve been using implicit dependencies until now, where the VM depended upon the network interface, and the network interface depended upon the subnet. The subnet depended upon the virtual network, and all of these resources depended on the resource group. These dependencies naturally occur when we use one resource’s output as another’s input.

However, sometimes, we want to define an explicit dependency on a resource, especially when there is no way to define an implicit dependency on it. You can use the depends_on attribute for that kind of operation.

Tip

Avoid explicit dependencies unless needed, as Terraform uses parallelism to manage resources. If explicit dependencies are not required, it will slow down Terraform runs because it can process multiple parallel resources.

To visualize the dependencies between resources, we can export a graph from the state file and convert that into a PNG file using a tool such as Graphviz.

Run the following command to export the dependency graph:
$ terraform graph > vm.dot

We can then process the graph file using the Graphviz tool. To install the tool on Ubuntu, run the following command:
$ sudo apt install graphviz -y

Now run the following command to convert the graph file into a PNG file:
$ cat vm.dot | dot -T png -o vm.png

The graph is available at https://github.com/PacktPublishing/Modern-DevOps-Practices-2e/blob/main/ch8/terraform-graph.png. Now, let’s go ahead and see how we can clean up our resources.

Cleaning up resources

As we already know, we run the following command to clean up the resources:
$ terraform destroy –auto-approve

It will clear resources from the resource group and delete the resource group after that.

While using terraform destroy can be an easy way to eliminate resources you don’t need, it is best if you stick to this only in the dev environment and never use it in production. Instead, you can remove resources you don’t need from the configuration and then run terraform apply.

Summary

In this chapter, we’ve discussed Terraform’s core and understood some of the most common commands and functionalities from a hands- on perspective. We started with understanding IaC, introduced Terraform as an IaC tool, installed Terraform, understood Terraform providers, and used the Azure Terraform provider to manage infrastructure in Azure.

We then looked at Terraform variables and multiple ways of supplying values to the variables. We discussed the core Terraform workflow and several commands you would use to manage infrastructure using Terraform. We then looked at Terraform modules and then at Terraform state as an essential component that helps Terraform keep track of the infrastructure it is managing.

We looked at local and remote state storage and used Azure Blob Storage as the remote state backend. We then discussed Terraform workspaces and how they enable us to use the same Terraform configuration to build multiple environments with hands-on exercises.

We then looked at some advanced operations with Terraform state using the outputs, state, and console commands. We finally looked at how Terraform manages dependencies and viewed a dependency graph using the graph command.

In the next chapter, we will delve into configuration management using Ansible.

Leave a Reply

Your email address will not be published. Required fields are marked *