Skip to content

Instantly share code, notes, and snippets.

@Fabaderheld
Created September 6, 2022 04:50
Show Gist options
  • Save Fabaderheld/2c0ae23b828cff44eef16c72fef6c070 to your computer and use it in GitHub Desktop.
Save Fabaderheld/2c0ae23b828cff44eef16c72fef6c070 to your computer and use it in GitHub Desktop.
Create Terraform storagebackend

Connect-AzAccount

Create Azure Service Princal

$sp = New-AzADServicePrincipal -DisplayName "terraform" -Role "Contributor" $sp.AppId $sp.PasswordCredentials.SecretText

Create Terraform storage backend

$RESOURCE_GROUP_NAME='tfstate' $STORAGE_ACCOUNT_NAME="tfstate$(Get-Random)" $CONTAINER_NAME='tfstate'

Create resource group

New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location eastus

Create storage account

$storageAccount = New-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME -SkuName…

Create blob container

$storageAccount = New-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME -SkuName Standard_LRS -Location westeurope -AllowBlobPublicAccess $true# New-AzStorageContainer -Name $CONTAINER_NAME -Context $storageAccount.context -Permission blob

Set env variables

$ACCOUNT_KEY=(Get-AzStorageAccountKey -ResourceGroupName $RESOURCE_GROUP_NAME -Name $STORAGE_ACCOUNT_NAME)[0].value $env:ARM_ACCESS_KEY=$ACCOUNT_KEY $ACCOUNT_KEY

$env:ARM_CLIENT_ID="<service_principal_app_id>" $env:ARM_SUBSCRIPTION_ID="<azure_subscription_id>" $env:ARM_TENANT_ID="<azure_subscription_tenant_id>" $env:ARM_CLIENT_SECRET="<service_principal_password>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment