Setup and Run Terraform for Azure

·

1 min read

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}
  1. Create a shell script to add the storage account for storing state file. [Optional]

  2. Set the env variable ARM_Access_Key to access storage.

  3. Create one container per project for state files of each env.

  4. Run terraform init to initialize the backend on storage account.

  5. Go through & validate tfvars files.

  6. 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