Friday, September 9, 2022

Terraform State List and Terraform Graph

This blog explains two useful commands available in Terraform.

Tip 01

You might have a requirement to view resources in terraform state file to get an idea on resources already applied with your Terraform script. But, if you try to do it by reading the state file, it is not an easy task. You can get list of resources available in Terraform state file with a simple command as explain below.

Pre-requisites: Terraform state file with resource info

  • Go to your working folder and run following command

    terraform state list

  • It will list down all the resources available in the state file


Tip 02:

You can use Terraform plan to do dry run in Terraform which gives an overview of the resources going to be created, changed and destroyed. However, if you want to get a graphical view of the resources before applying, you can use following command.

    terraform graph

It will generate a DOT format code as below. 

digraph {

        compound = "true"

        newrank = "true"

        subgraph "root" {

                "[root] azurerm_app_service.appservice (expand)" [label = "azurerm_app_service.appservice", shape = "box"]

                "[root] azurerm_app_service_plan.demoapp-plan (expand)" [label = "azurerm_app_service_plan.demoapp-plan", shape = "box"]

                "[root] azurerm_app_service_plan.demoapp-plan02 (expand)" [label = "azurerm_app_service_plan.demoapp-plan02", shape = "box"]

                "[root] azurerm_resource_group.rg (expand)" [label = "azurerm_resource_group.rg", shape = "box"]

                "[root] azurerm_resource_group.rg02 (expand)" [label = "azurerm_resource_group.rg02", shape = "box"]

                "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"]" [label = "provider[\"registry.terraform.io/hashicorp/azurerm\"]", shape = "diamond"]

                "[root] var.environment" [label = "var.environment", shape = "note"]

                "[root] var.location" [label = "var.location", shape = "note"]

                "[root] var.prefix" [label = "var.prefix", shape = "note"]

                "[root] azurerm_app_service.appservice (expand)" -> "[root] azurerm_app_service_plan.demoapp-plan (expand)"

                "[root] azurerm_app_service_plan.demoapp-plan (expand)" -> "[root] azurerm_resource_group.rg (expand)"

                "[root] azurerm_app_service_plan.demoapp-plan02 (expand)" -> "[root] azurerm_resource_group.rg02 (expand)"

                "[root] azurerm_resource_group.rg (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"]"

                "[root] azurerm_resource_group.rg (expand)" -> "[root] var.environment"

                "[root] azurerm_resource_group.rg (expand)" -> "[root] var.location"

                "[root] azurerm_resource_group.rg (expand)" -> "[root] var.prefix"

                "[root] azurerm_resource_group.rg02 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"]"

                "[root] azurerm_resource_group.rg02 (expand)" -> "[root] var.environment"        

                "[root] azurerm_resource_group.rg02 (expand)" -> "[root] var.location"

                "[root] azurerm_resource_group.rg02 (expand)" -> "[root] var.prefix"

                "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"] (close)" -> "[root] azurerm_app_service.appservice (expand)"

                "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"] (close)" -> "[root] azurerm_app_service_plan.demoapp-plan02 (expand)"

                "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/azurerm\"] (close)"

        }

}

You can copy DOT formatted code to a GraphViz tool and generate a graph.



The graph contains graphical representation of the resources.



No comments:

Post a Comment