Connect to Azure with Terraform
The azurerm documentation in the Terraform Registry lists different ways to authenticate to Azure. In this tutorial, we’ll authenticate with the Azure CLI. If you want to automate this process, you authenticate with a service principal or managed service identity instead.
This page describes how to log into the Azure CLI and set up your azurerm
Terraform provider.
Log into the Azure CLI
-
In your terminal, run the following command:
az login
A browser window opens at
https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize
.Select your Microsoft account to log in.
If a window does not open, cancel the command (Ctrl + C) and run the following command instead:
az login --use-device-code
Then follow the directions in the terminal to log in.
-
From the table in your terminal, select your subscription.
If you have only one subscription, press Enter.
If you have more than one subscription, enter the number in the left-most column of your desired subscription's row and then press Enter.
If the terminal shows your tenant and subscription ID, you've successfully logged in to the Azure CLI. You may seem some announcements or warnings, as well.

Create a providers.tf
file
-
Create a folder called
azure-web-app-with-terraform
.mkdir azure-web-app-with-terraform
-
Change directories into that folder.
cd azure-web-app-with-terraform
-
Create a new file and enter the following Terraform code:
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "4.9.0" } } } provider "azurerm" { features {} subscription_id = <YOUR-AZURE-SUBSCRIPTION-ID> }
-
Save this file as
providers.tf
.This file defines your Terraform providers. You can define any number of providers in your Terraform project. For this tutorial, we’ll use only the
azurerm
provider.
Initialize your Terraform workspace
In your terminal, run the following command:
terraform init
Confirmation TBD
Troubleshooting
TBD
Learn more
TBD