Create a and Use `terraform fmt` to automatically format the files.


In this lab you will

  • create a and a
  • use terraform fmt to tidy them up

Starting point

Your files should currently look like this:


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

Open the editor

  1. Open the Cloud Shell

    Authenticate and check you are in the right subscription.

  2. Change to your working directory

    cd ~/terraform-basics
  3. Create empty and files


    The touch command updates the modified timestamp on linux files. If the file does not exists then it also creates files.

  4. Open the Monaco editor

    code .

    Using code . opens the current working directory.

Create the initial

  1. Copy the code block

    resource azurerm_resource_group basics {
    name =
    location="West Europe"
  2. Select the file in the editor’s file browser

  3. Paste

  4. Save the file

terraform fmt

The format used within Terraform files is called HashiCorp Configuration Language or HCL for short. The formatting of these files is very standardised and has been hardcoded into the terraform binary itself. (It is based on the principles of gofmt, which is not that surprising as terraform is written in Go.)

  1. Run terraform fmt

    terraform fmt

    The command will find all valid *.tf files in the current working directory and will format them correctly. The command outputs the filenames of any modified files.
  2. Reselect the

    Click on the filename in the editor’s file browser.

    The file should now have indentation, alignment and the resource type and name are now quoted correctly.


    resource "azurerm_resource_group" "basics" {
      name     =
      location = "West Europe"

Create a

  1. Copy and paste into

    variable "resource_group_name" {
      description = "Name for the resource group"
      type        = "string"
      default     = "terraform-basics"
  2. Save the file

  3. Rerun terraform fmt and reselect the file in the editor.

    terraform fmt

    The up cursor key recalls previous commands.

    You’ll see the value for the type argument, string does not need quotes.

    Variables can be strings, booleans, numbers or more complex types. We’ll cover variables in more depth later.


We have reached the end of the lab. You have created a couple of files and used terraform fmt to fix common formatting issues.

Move onto the next lab and we’ll customise and validate the files.

Help us improve

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

 Make a change