Setup and Run Terraform for Azure
Authentication
Service Principal
$env:ARM_CLIENT_ID=''
$env:ARM_CLIENT_SECRET=''
$env:ARM_SUBSCRIPTION_ID=''
$env:ARM_TENANT_ID=''
export ARM_CLIENT_ID=''
export ARM_CLIENT_SECRET=''
export ARM_SUBSCRIPTION_ID=''
export ARM_TENANT_ID=''
With Login
az login
az account set --subscription {subscription_id}
Create a shell script to add the storage account for storing state file. [Optional]
Set the env variable
ARM_Access_Key
to access storage.Create one container per project for state files of each env.
Run
terraform init
to initialize the backend on storage account.Go through & validate
tfvars
files.Follow the below steps.
Set the access keys for remote backend
LINUX
export ARM_ACCESS_KEY=''
WINDOWS
$env:ARM_ACCESS_KEY=$(az keyvault secret show --name backend_key --vault-name mykeyvault --query value -o tsv)
set ARM_ACCESS_KEY=''
Initialize the remote backend on azure
terraform init -backend-config=backends/backend-{project}-{env}.tf
Terraform Plan with right tfvar file
terraform plan -var-file=environments/{project}-{env}.tfvars -out plan.tfplan
Terrafor Apply
terraform apply plan.tfplan