Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Setting Up Azure Storage to Be Used for Terraform Remote State

Hey, Gurus! Welcome to the Setting Up Azure Storage to Be Used for Terraform Remote State lab. In this lab, we will cover 4 objectives: 1. First, we will log into the Azure portal and then configure the Cloud Shell to use bash. 1. Second, we will download and run a script to setup the lab environment. 1. Third, we will create an Azure Storage account with a storage container to use for remote storage of our Terraform state file. 1. And for our fourth objective, we will configure Terraform to use Azure Storage to remotely store our state. It is considered best practice to remotely store your state since it helps keep the file secure as it may contain sensitive data in plain text. It also allows for collaboration when managing and working with resources in a team environment.

Azure icon
Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 30m
Published
Clock icon Jun 17, 2022

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Set Up the Cloud Shell

    In the Portal

    1. Go to the portal and log in using your lab credentials.
    2. Click the Cloud Shell icon next to the search bar in the portal.
    3. Select Bash at the prompt.
    4. Click Show Advanced Settings.
    5. Set the Cloud Shell region to the same location as the resource group.
    6. Select the existing Resource Group, and select Use Existing for the Storage Account.
    7. In the File share section, choose Create new and enter "terraform".
    8. Click Attach Storage.
  2. Challenge

    Set Up the Lab Environment

    In the Cloud Shell

    1. Download the lab_3_setup.sh script at https://github.com/ACloudGuru/advanced-terraform-with-azure/raw/main/lab_setting_up_azure_storage_to_be_used_for_terraform_remote_state/lab_3_setup.sh.
    2. Add execute permissions to the script.
    3. Run the lab_3_setup.sh script.
    4. Move to the terraformguru directory.
    5. Initialize the working directory.
  3. Challenge

    Create the Azure Storage Account Configuration

    In the Cloud Shell

    1. Import the resource group found in the remote-state-storage.tf file.
    2. Look up and add the resource group name and location values to the resource block.
    3. Add the following resource blocks to the configuration and save the file:
      resource "azurerm_storage_account" "tfstate" {
        name                     = "tfstate${random_string.resource_code.result}"
        resource_group_name      = azurerm_resource_group.guru.name
        location                 = azurerm_resource_group.guru.location
        account_tier             = "Standard"
        account_replication_type = "LRS"
        allow_blob_public_access = true
      
        tags = {
          environment = "dev"
        }
      }
      
      resource "azurerm_storage_container" "tfstate" {
        name                  = "tfstate"
        storage_account_name  = azurerm_storage_account.tfstate.name
        container_access_type = "blob"
      }
      
      # Outputs
      output "storage_account_name" {
         value = azurerm_storage_account.tfstate.name
      }
      
      output "storage_container_name" {
        value = azurerm_storage_container.tfstate.name
      }
      
    4. Check your formatting and validate your code.
    5. Deploy your resources and copy the values in the output.
  4. Challenge

    Deploy the Azure Storage Account with Terraform

    1. Once successfully deployed, create a file called backend.tf and add the following code:

      terraform {
        backend "azurerm" {
          resource_group_name  = "<RESOURCE_GROUP_NAME>"
          storage_account_name = "<STORAGE_ACCOUNT_NAME>"
          container_name       = "CONTAINER_NAME"
          key                  = "terraform.tfstate"
        }
      }  
      
    2. Replace the <RESOURCE_GROUP_NAME> and <STORAGE_ACCOUNT_NAME> placeholders with the values from the outputs of the storage account deployment.

    3. Save the file.

    4. Run the terraform init command to add your remote state backend configuration.

    5. Verify that your state is now being stored remotely.

    6. Once verified, delete your local state file.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans