Skip to the content.
Back to Lab Index

Lab description

This lab will cover the very basic terraform workflow for the updating of resources in Azure.

The terraform Core Workflow is still:

Day n operation (destroy)

Setup

Make sure you are in the correct folder:

1
cd ~/terraform-labs
1
cd ~/terraform-labs

If you were unable to complete the last lab, you can find a copy of the files in the solutions folder

1. Plan

Run a plan to see what will be destroyed.

1
terraform plan -destroy -out contoso.tfplan
1
terraform plan -destroy -out contoso.tfplan

Your plan should look something like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
azurerm_resource_group.contoso_rg: Refreshing state... [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # azurerm_resource_group.contoso_rg will be destroyed
  - resource "azurerm_resource_group" "contoso_rg" {
      - id         = "/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg" -> null
      - location   = "uksouth" -> null
      - name       = "contoso_rg" -> null
      - tags       = {
          - "cost_center" = "contoso research"
        } -> null
        # (1 unchanged attribute hidden)
    }

Plan: 0 to add, 0 to change, 1 to destroy.

───────────────────────────────────────────────────────

Saved the plan to: contoso.tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "contoso.tfplan"

2. Destroy

If the plan looks as expected, go ahead and remove the resource group that we created using destroy operation.

Destroy depends on your state file to decide what needs to be removed.

Note that a shortcut to performing a destroy plan and apply is terraform destroy.

1
terraform apply "contoso.tfplan"
1
terraform apply "contoso.tfplan"

You should see a terminal output like this:

1
2
3
4
5
azurerm_resource_group.contoso_rg: Destroying... [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg]
azurerm_resource_group.contoso_rg: Still destroying... [id=/subscriptions/b857908d-3f5c-4477-91c1-0fbd08df4e88/resourceGroups/contoso_rg, 10s elapsed]
azurerm_resource_group.contoso_rg: Destruction complete after 15s

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

3. Verify

Verify that the contoso_rg resource group has been deleted from your azure subscription.

1
az group show --name "contoso_rg"
1
az group show --name "contoso_rg"

The state file should now be empty as we have completely cleaned up our terraform managed infrastructure.

1
terraform show terraform.tfstate
1
terraform show terraform.tfstate

4. Recap

Here is a list of commands covered so far:

If you have managed to finish this lab ahead of time, feel free to spend some time around the docs and try out above commands with other options.

Just make sure to clean up the infrastructure prior to next lab.

5. Graph (optional bonus lab)

Take a look at the graph command. Terraform builds a dependency graph from the Terraform configurations, and walks this graph to generate plans, refresh state, and more

See:

To see the graph in svg format, graphviz needs to be installed. This will not work in cloud shell as there isn’t sudo access, but should work on vs code online. (or local environments) http://www.graphviz.org/download.


Back to Lab Index