terraform version
az login
Storage Blob Data Contributor role needed
Provision the Azure Blob Storage backend for Terraform state. This is often done once per organization with a bootstrap script.
1 #!/bin/bash 2 # Bootstrap Terraform remote state backend 3 set -e 4 5 RESOURCE_GROUP="tfstate-rg" 6 LOCATION="eastus" 7 STORAGE_ACCOUNT="tfstate$(openssl rand -hex 4)" # Unique name 8 CONTAINER_NAME="tfstate" 9 10 echo "Creating resource group..." 11 az group create --name $RESOURCE_GROUP --location $LOCATION 12 13 echo "Creating storage account: $STORAGE_ACCOUNT" 14 az storage account create \ 15 --name $STORAGE_ACCOUNT \ 16 --resource-group $RESOURCE_GROUP \ 17 --location $LOCATION \ 18 --sku Standard_LRS \ 19 --encryption-services blob \ 20 --min-tls-version TLS1_2 \ 21 --allow-blob-public-access false 22 23 echo "Enabling blob versioning..." 24 az storage account blob-service-properties update \ 25 --resource-group $RESOURCE_GROUP \ 26 --account-name $STORAGE_ACCOUNT \ 27 --enable-versioning true \ 28 --enable-delete-retention true \ 29 --delete-retention-days 30 30 31 echo "Creating container..." 32 az storage container create \ 33 --name $CONTAINER_NAME \ 34 --account-name $STORAGE_ACCOUNT \ 35 --auth-mode login 36 37 echo "\n=== Backend config ===" 38 echo "resource_group_name = \"$RESOURCE_GROUP\"" 39 echo "storage_account_name = \"$STORAGE_ACCOUNT\"" 40 echo "container_name = \"$CONTAINER_NAME\"" 41 echo "key = \"<your-project>.terraform.tfstate\""
Storage account and container created for Terraform state
Sign in to share your feedback and join the discussion.