Plan

Start working with terraform.tfvars to specify your variable values and then use `terraform plan` to display the actions that Terraform will take.

Overview

In this lab you will

  • create a terraform.tfvars
  • specify the value for location
  • display a plan

Starting point

Your files should currently look like this:

  • provider.tf

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.1"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    
      storage_use_azuread = true
    }
    
  • variables.tf

    variable "resource_group_name" {
      description = "Name for the resource group"
      type        = string
      default     = "terraform-basics"
    }
    
    variable "location" {
      description = "Azure region"
      type        = string
      default     = "West Europe"
    }
    
    
  • main.tf

    resource "azurerm_resource_group" "basics" {
      name     = var.resource_group_name
      location = var.location
    }
    

terraform.tfvars

The variables.tf file has been used to declare the variables, including their type and default values. A more complete configuration may add information such as a description and validation criteria.

When planning a deployment then you usually specify the values for those variables using a terraform.tfvars file.

There are other ways of specifying variables, such as via command line arguments, environment variables and *.auto.tfvars files.

  1. Create a terraform.tfvars file

    touch terraform.tfvars
    
  2. Click on the refresh icon in the editor’s file explorer

  3. Specify the location

    ⚠️ Change the value of location to your local region.

    If that region is West Europe then specify North Europe.

    In the example below I have used my local region:

    location = "UK South"
    

    Note that the value of the resource_group_name is not specified and will default to terraform-basics as per the variables.tf file.

  4. Close the editor

terraform plan

  1. Run terraform plan

    This is one of the most commonly used terraform commands, and shows which resources will be create, destroyed, modified or recreated.

    terraform plan
    

    Example output:

    
    Terraform used the selected providers to generate the following execution
    plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # azurerm_resource_group.basics will be created
      + resource "azurerm_resource_group" "basics" {
          + id       = (known after apply)
          + location = "uksouth"
          + name     = "terraform-basics"
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ─────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    

    The output shows that the resource group will be created.

Summary

You have started using the terraform.tfvar and have displayed the output from terraform plan.

In the next lab we will finally create something and will then explore the state file.


Help us improve

Azure Citadel is a community site built on GitHub, please contribute and send a pull request

 Make a change