Plan fails with "Error: Unable to list provider registration status"

While running the steps at Build Infrastructure - Terraform Azure Example | Terraform | HashiCorp Developer to create a resource group in Azure, terraform plan freezes. Only when hitting Ctrl+C to cancel, the following error is prompted:

user@machine:~/learn-terraform-azure$ terraform plan
^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

Stopping operation...

Planning failed. Terraform encountered an error while generating this plan.

│ Error: Unable to list provider registration status, it is possible that this is due to invalid credentials or the service principal does not have permission to use the Resource Manager API, Azure error: resources.ProvidersClient#List: Failure sending request: StatusCode=0 -- Original Error: context canceled
│   with provider["registry.terraform.io/hashicorp/azurerm"],
│   on main.tf line 13, in provider "azurerm":
│   13: provider "azurerm" {
│

The main.tf file has been set as follows:

# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
  }

  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myTFResourceGroup"
  location = "australiaeast"
}

Service principal has been created.

az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<SUBSCRIPTION_ID>"

and environmental variables have been set

user@machine:~/learn-terraform-azure$ set | grep ARM
ARM_CLIENT_ID=<some id>
ARM_CLIENT_SECRET=<some password>
ARM_SUBSCRIPTION_ID=<subscription id>
ARM_TENANT_ID=<tenant id>

Not sure if relevant, but I am runnning terraform on Ubuntu.

user@machine:~/learn-terraform-azure$ terraform --version
Terraform v1.4.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.0.2

What could be causing the issue?

I’m running into the same issue. I verified that the service principal has the Contributor rule assigned and gave plenty of time for it to apply while troubleshooting. I am using Ubuntu through WSL on Windows 11.

AZ CLI Version

az --version
azure-cli                         2.47.0

core                              2.47.0
telemetry                          1.0.8

Dependencies:
msal                              1.20.0
azure-mgmt-resource               22.0.0

Python location '/opt/az/bin/python3'
Extensions directory '/home/user/.azure/cliextensions'

Python (Linux) 3.10.10 (main, Mar 31 2023, 09:26:59) [GCC 11.3.0]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.

Terraform Version

terraform version
Terraform v1.4.4
on linux_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.0.2

Something interesting I found was if you skip ahead in the Azure tutorial to the cloud section I am able to run terraform commands like terraform plan, apply, and destroy and it works without issue. Maybe it is a bug in the Ubuntu version of Terraform?

I am also having this issue running on Ubuntu WSL. Tried manually creating the service principal also. Will test out just doing it on powershell.