- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
Set Up the Cloud Shell
In the Portal
- Go to the portal and log in using your lab credentials.
- Click the Cloud Shell icon next to the search bar in the portal.
- Select Bash at the prompt.
- Click Show Advanced Settings.
- Set the Cloud Shell region to the same location as the resource group.
- Select the existing Resource Group, and select Use Existing for the Storage Account.
- In the File share section, choose Create new and enter "terraform".
- Click Attach Storage.
-
Challenge
Set Up the Lab Environment
In the Cloud Shell
- Download the
lab_3_setup.sh
script athttps://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
. - Add execute permissions to the script.
- Run the lab_3_setup.sh script.
- Move to the
terraformguru
directory. - Initialize the working directory.
- Download the
-
Challenge
Create the Azure Storage Account Configuration
In the Cloud Shell
- Import the resource group found in the
remote-state-storage.tf
file. - Look up and add the resource group name and location values to the resource block.
- 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 }
- Check your formatting and validate your code.
- Deploy your resources and copy the values in the output.
- Import the resource group found in the
-
Challenge
Deploy the Azure Storage Account with Terraform
-
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" } }
-
Replace the <RESOURCE_GROUP_NAME> and <STORAGE_ACCOUNT_NAME> placeholders with the values from the outputs of the storage account deployment.
-
Save the file.
-
Run the
terraform init
command to add your remote state backend configuration. -
Verify that your state is now being stored remotely.
-
Once verified, delete your local state file.
-
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.