Instead of giving Terraform access to my entire Azure subscription, I want to use resource groups as an isolation and security boundary.
I got tired of doing this by hand and decided to (semi-)automate it.
Each "environment" here is an independent workload.
- multiple terraform environments
- each environment has own resource group
- each environment has own service principal
- service principal has
Contributor
rights on its own resource group
- resource group
- service principal for that resource group
- storage account in that resource group which you can use for Terraform state.
- Logged in via
az login
Owner
rights on your subscription
We will save our Terraform state in Azure Blob Storage
- Create a storage account to hold Terraform state for this project. Be sure to disable public read access.
- Generate SAS token for this storage account
- Create Blob Storage container, e.g.
workspaces
,projects
Create an azure.conf
file, using azure.conf.sample
as a template, filling in the placeholders iwth your values.
storage_account_name="azurestorageaccountname"
container_name="storagecontainername"
key="project.tfstate"
sas_token="?sv=2019-12-12…"
Run init
with our config.
terraform init -backend-config=./azure.conf
terraform plan
terraform apply
- Create custom "Terraform Contributor" role for service principal so that it can also assign RBAC. Example use case is AAD Pod Identity
- Save service principal secrets to Key Vault instead of outputting them