Set Up your first Terraform environment
Prerequisites​
- IDE (Vscode)
- TF executable
- Azure Subscription
- Storage Account
- Azure CLI
Go​
Define the subscription to store the state
Download the latest TF version here: Terraform Versions | HashiCorp Releases
Add it to the path
Type to test: terraform --version
Create a new SPN or MI
Test connection
Create a container to store the tfstate
Store Terraform state in Azure Storage Microsoft Learn
$RESOURCE_GROUP_NAME='Terraform-RG'
$STORAGE_ACCOUNT_NAME="tfstate914400870"
$CONTAINER_NAME='tfstate'
$LOCATION = Â "westeurope"
Create resource group
New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location $LOCATION
Create storage account
$storageAccount = New-AzStorageAccount -ResourceGroupName $RESOURCE_GROUP_NAME -Name
$STORAGE_ACCOUNT_NAME -SkuName Standard_LRS -Location westeurope -AllowBlobPublicAccess $true
Create blob container
New-AzStorageContainer -Name $CONTAINER_NAME -Context $storageAccount.context -Permission blob
$ACCOUNT_KEY=(Get-AzStorageAccountKey -ResourceGroupName $RESOURCE_GROUP_NAME -Name
$STORAGE_ACCOUNT_NAME)[0].value
$env:ARM_ACCESS_KEY=$ACCOUNT_KEY
Add permissions
Create your first TF file
az login --use-device-code
set the target subscription
az account set --subscription="<subid>"
type terraform init
The state file is now created in the storage account
terraform plan
terraform apply -auto-approve