Install and configure Terraform to provision VMs and other infra to Azure

Note: if you don’t want to install terraform locally then use Azure Cloud Shell

use the >_ icon in the Azure portal

Set up Terraform access to Azure

1. get your subscription ID and tenant ID

az login

az account show --query "{subscriptionId:id, tenantId:tenantId}"

and set via an environment variable:

export SUBSCRIPTION_ID=abcd-abcd-etc
az account set --subscription="${SUBSCRIPTION_ID}"

2. create an Azure AD service principal

(an Azure AD service principal is a credential for your application – )

az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}"

and note your appId, tenantID and password.

Slot them into this Terraform script ( to set up env vars:

echo “Setting environment variables for Terraform”
export ARM_SUBSCRIPTION_ID=your_subscription_id
export ARM_CLIENT_ID=your_appId
export ARM_CLIENT_SECRET=your_password
export ARM_TENANT_ID=your_tenant_id

# Not needed for public, required for usgovernment, german, china
export ARM_ENVIRONMENT=public

Note: remember to apply these environment variables to your current shell. i.e. use:

. ./

(notice the leading dot?)

Create a file with:

provider "azurerm" {
resource "azurerm_resource_group" "rg" {
    name = "testResourceGroup"
    location = "westus"

and run with terraform initterraform plan and terraform apply

This should create a Resource Group.

Leave a Reply

Your email address will not be published. Required fields are marked *