Getting Started - Azure

Change Infrastructure

In the previous page, you created your first infrastructure with Terraform: a resource group. In this lesson, you'll modify that resource and see how Terraform handles change.

Infrastructure is continuously evolving, and Terraform was built to help manage and enact that change. As you change Terraform configurations, Terraform builds an execution plan that only modifies what is necessary to reach your desired state. Terraform builds an execution plan by comparing your desired state (changed configuration) to the current state, saved in the terraform.tfstate file.

By using Terraform to change infrastructure, you can version control not only your configurations but also your state so you can see how the infrastructure evolved over time.

Configuration

Let's modify the resource group of our instance by adding metadata (tags).

In the Cloud Shell, in the directory that contains the configuration file, type code main.tf. This will open the configuration file in the Cloud Shell editor.

Edit the azurerm_resource_group resource in your configuration and change it to the following:

resource "azurerm_resource_group" "rg" {
    name     = "myTFResourceGroup"
    location = "eastus"

    tags {
        environment = "TF sandbox"
    }
}

Use the ... menu on the top right of the editor window to save your changes and close the editor.

Terraform configurations are meant to be changed like this. You can also completely remove resources and Terraform will know to destroy the old one.

Some changes will force Terraform to destroy and then recreate the resource. For example, changing the name or location of a resource group will force Terraform to recreate the resource. Recreating the resource group will force the destruction of all the resources in the group. Use caution when making changes that force Terraform to destroy and recreate a resource.

Apply Changes

After changing the configuration, run terraform apply again to see how Terraform will apply this change to the existing resources.

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  ~ azurerm_resource_group.rg
      tags.%:           "0" => "1"
      tags.environment: "" => "TF sandbox"


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

In this example, the tags block can be updated in-place (shown with the ~ prefix). The execution plan makes it clear what Terraform will do.

Terraform prompts for approval of the execution plan before proceeding. Answer yes to execute the planned steps:

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.rg: Modifying... (ID: /subscriptions/.../resourceGroups/myTFResourceGroup)
  tags.%:           "0" => "1"
  tags.environment: "" => "TF sandbox"
azurerm_resource_group.rg: Modifications complete after 2s

You can use terraform show again to see the new values associated with this resource group:

glenn@Azure:~/test$ terraform show
azurerm_resource_group.rg:
  id = /subscriptions/.../resourceGroups/myTFResourceGroup
  location = eastus
  name = myTFResourceGroup
  tags.% = 1
  tags.environment = TF sandbox