DEV Community

Jasper Rodda
Jasper Rodda

Posted on • Edited on

How to Login/Authenticate Terraform via Azure CLI

There are multiple ways for authenticating.

  1. Authenticating to Azure using the Azure CLI
  2. Authenticating to Azure using Managed Service Identity
  3. Authenticating to Azure using a Service Principal and a Client Certificate
  4. Authenticating to Azure using a Service Principal and a Client Secret
  5. Authenticating to Azure using a Service Principal and Open ID Connect

Official Terraform documentation recommends authenticating via Azure CLI when using locally and They recommend using either a Service Principal or Managed Service Identity when running Terraform non-interactively (such as when running Terraform in a CI server)

Pre-requisites

  1. Install Azure CLI

Step 1: Login

Login using powershell and use command az login

Step 2: Microsoft prompt

A prompt is opened in browser, and you can login using the credentials.

Step 3: PowerShell Json Response

Notice you will get below response after authorization.

PS C:\Users\Jasper> az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "3hfdg567-4564-6u78-k898-asdgesr34523",
    "id": "3hfdg567-4564-6u78-k898-asde8sr34523",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Azure subscription 1",
    "state": "Enabled",
    "tenantId": "3hfdg567-4564-6u78-k898-asdgesr34523",
    "user": {
      "name": "pemail@gmail.com",
      "type": "user"
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)