{"id":79,"date":"2025-07-23T20:08:40","date_gmt":"2025-07-23T17:08:40","guid":{"rendered":"https:\/\/www.martinsgalins.lv\/?p=79"},"modified":"2025-07-28T20:08:43","modified_gmt":"2025-07-28T17:08:43","slug":"quick-setup-terraform-remote-state-file-in-azure-with-powershell","status":"publish","type":"post","link":"https:\/\/www.martinsgalins.lv\/?p=79","title":{"rendered":"Quick Setup: Terraform Remote State File in Azure with PowerShell"},"content":{"rendered":"\n<p>\u26a0\ufe0f <strong>Disclaimer<\/strong>: This script is for quick testing or learning purposes only. Do <strong>not<\/strong> use it in production environments. There are more secure authentication methods available for accessing Azure Storage Accounts.<\/p>\n\n\n\n<p>This PowerShell script automates the deployment of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Resource Group<\/li>\n\n\n\n<li>A Storage Account<\/li>\n\n\n\n<li>A Blob Container<\/li>\n<\/ul>\n\n\n\n<p>PowerShell Script<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Connect-AzAccount -DeviceCode\n# Define variables\n$subscriptionId = \"xxxxx\"\n$resourceGroupName = \"test-terraformbackend\"\n$location = \"swedencentral\"\n$storageAccountName = \"xxxxx\" # must be globally unique and lowercase\n$containerName = \"tfstate\"\n$statefile = \"statefile.tfstate\"\n\n# Set the context to the correct subscription\nSet-AzContext -SubscriptionId $subscriptionId\n\n# Create the resource group (if it doesn't exist)\nif (-not (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue)) {\n    Write-Host \"Creating resource group '$resourceGroupName'...\"\n    New-AzResourceGroup -Name $resourceGroupName -Location $location\n}\n\n# Create the storage account\nWrite-Host \"Creating storage account '$storageAccountName'...\"\n$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName `\n    -Name $storageAccountName `\n    -Location $location `\n    -SkuName Standard_LRS `\n    -Kind StorageV2\n\n# Get the storage account context\n$ctx = $storageAccount.Context\n\n# Create the blob container\nWrite-Host \"Creating blob container '$containerName'...\"\nNew-AzStorageContainer -Name $containerName -Context $ctx\n\n# Retrieve the storage account key\n$keys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName\n$accessKey = $keys&#91;0].Value\n\n# Output in the requested format\nWrite-Host \"`n# === Terraform-style Output ===\"\nWrite-Host \"access_key           = `\"$accessKey`\"\"\nWrite-Host \"storage_account_name = `\"$storageAccountName`\"\"\nWrite-Host \"container_name       = `\"$containerName`\"\"\nWrite-Host \"container_name       = `\"$statefile`\"\"<\/code><\/pre>\n\n\n\n<p>Update your Terraform <code>backend \"azurerm\"<\/code> block with the values output by the PowerShell script:<\/p>\n\n\n\n<p>Terraform Configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    azurerm = {\n      source  = \"hashicorp\/azurerm\"\n      version = \">=4.33.0\"\n    }\n  }\n  backend \"azurerm\" {\n    access_key           = \"xxxx\"\n    storage_account_name = \"xxxx\"\n    container_name       = \"tfstate\"\n    key                  = \"statefile.tfstate\"\n  }\n}\nprovider \"azurerm\" {\n  features {}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u26a0\ufe0f Disclaimer: This script is for quick testing or learning purposes only. Do not use it in production environments. There are more secure authentication methods available for accessing Azure Storage Accounts. This PowerShell script automates the deployment of: PowerShell Script Update your Terraform backend &#8220;azurerm&#8221; block with the values output by the PowerShell script: Terraform [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[4,5,9],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-azure","tag-azure","tag-azure-storageaccount","tag-terraform"],"_links":{"self":[{"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":3,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.martinsgalins.lv\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}