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

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

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

azure-cli-finish

Create a providers.tf file

  1. Create a folder called azure-web-app-with-terraform.

     mkdir azure-web-app-with-terraform
    
  2. Change directories into that folder.

     cd azure-web-app-with-terraform
    

    To get your Azure subscription ID, run the following command in your terminal:

    az account show

    This returns a JSON response in your terminal. Your subscription ID is in the id field.

  3. 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>
     }
    
  4. 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

Updated: