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.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- 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.
- 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
- 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
- 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 theterraform 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.