Professional Documents
Culture Documents
You have 1 free member-only story left this month. Sign up for Medium and get an extra one
Save
We can use remote backends, such as Azure Storage, Google Cloud Storage,
Amazon S3, and HashiCorp Terraform Cloud & Terraform Enterprise, to keep our
files safe and share between multiple users.
In this story, we will take a look at a step by step procedure to use Microsoft Azure
Storage to create a Remote Backend for Terraform using Azure CLI, PowerShell, and
Terraform.
53 3
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 1/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
az login
The process will launch the browser and after the authentication is complete we are
ready to go.
We will use the following command to get the list of Azure subscriptions:
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 2/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
We can select the subscription using the following command (both subscription id
and subscription name are accepted):
Then create the service principal account using the following command:
az ad sp create-for-rbac --role="Contributor"
--scopes="/subscriptions/SUBSCRIPTION_ID"
Note: as an option, we can add the -name parameter to add a descriptive name.
az ad sp create-for-rbac --role="Contributor"
--scopes="/subscriptions/SUBSCRIPTION_ID" --name="Azure-DevOps"
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 3/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
RESOURCE_GROUP_NAME=kopicloud-tstate-rg
STORAGE_ACCOUNT_NAME=kopicloudtfstate$RANDOM
CONTAINER_NAME=tfstate
# Create resource group
az group create --name $RESOURCE_GROUP_NAME --location "West Europe"
# Variables
$azureSubscriptionId = "9c242362-6776-47d9-9db9-2aab2449703"
$resourceGroup = "kopicloud-tstate-rg"
$location = "westeurope"
$random = -join ((0..9) | Get-Random -Count 5 | % {$_})
$accountName = "kopicloudtfstate" + $random
$containerName = "tfstate"
# company
variable "company" {
type = string
description = "This variable defines the name of the company"
}
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 5/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
# environment
variable "environment" {
type = string
description = "This variable defines the environment to be built"
}
# azure region
variable "location" {
type = string
description = "Azure region where the resource group will be
created"
default = "north europe"
}
Then we create the az-remote-backend-main.tf file that will configure the storage
account:
name = "${lower(var.company)}tf${random_string.tf-name.result}"
resource_group_name = azurerm_resource_group.state-rg.name
location = azurerm_resource_group.state-rg.location
account_kind = "StorageV2"
account_tier = "Standard"
access_tier = "Hot"
account_replication_type = "ZRS"
enable_https_traffic_only = true
lifecycle {
prevent_destroy = true
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 6/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
tags = {
environment = var.environment
}
}
# Create a Storage Container for the Core State File
resource "azurerm_storage_container" "core-container" {
depends_on = [azurerm_storage_account.state-sta]
name = "core-tfstate"
storage_account_name = azurerm_storage_account.state-sta.name
}
Finally, we create the file az-remote-backend-output.tf file that will show the
output:
output "terraform_state_resource_group_name" {
value = azurerm_resource_group.state-rg.name
}
output "terraform_state_storage_account" {
value = azurerm_storage_account.state-sta.name
}
output "terraform_state_storage_container_core" {
value = azurerm_storage_container.core-container.name
}
We will create a configuration file with the credentials information. For this
example, I called the file azurecreds.conf. This is the content of the file:
ARM_SUBSCRIPTION_ID="9c242362-6776-47d9-9db9-2aab2449703"
ARM_CLIENT_ID = "a47dd585-d5c1-4ffe-8ea2-91f0681c55bc"
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 7/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
ARM_CLIENT_SECRET="88f9776a-0e3h-4b9e-afcc-b4h3f17ad20h"
ARM_TENANT_ID="c7e7beb6-8e7c-41e7-bc41-37ggg3e33b665"
then we create the file provider-main.tf and add the code to manage the Terraform
and the Azure providers:
And that’s all folks. If you liked this story, please show your support by 👏 this story.
Thank you for reading!
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 8/9
3/28/23, 1:05 PM How to Create an Azure Remote Backend for Terraform | by Guillermo Musumeci | Medium
Your email
Subscribe
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our
privacy practices.
https://gmusumeci.medium.com/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520 9/9