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 – https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-create-service-principals )

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 (tf_env_vars.sh) to set up env vars:

#!/bin/sh
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:

. ./tf_env_vars.sh

(notice the leading dot?)

Create a test.tf file with:

and run with terraform initterraform plan and terraform apply

This should create a Resource Group.

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure

Leave a Reply

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