Search code examples
azureterraformterraform-provider-azure

'terraform init' returns 404 'Resource Group not found' when it does exist


Since adding backend "azurerm" to my Terraform main.tf file it's now returning a 404 on the resource group created to maintain the state file.

I'm at a bit of a loss to explain why, the session is logged in to the correct tenant and subscription using Connect-AzAccount and Set-AzContext methods in the Az PowerShell module.

Here's my setup:

main.tf

## Terraform Configuration
terraform {

  # Azure Remote State
  backend "azurerm" {
    resource_group_name  = "abc-uat-tfstate"
    storage_account_name = "abcuattfstate"
    container_name       = "tfstate"
    key                  = "myapp.uat.tfstate"
  }

  # Provider Dependencies
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.0"
    }
  }
}

## Provider Configurations

# Azure
provider "azurerm" {
  subscription_id = var.subscriptionId
  features {}
}

...

When I run terraform init on this main.tf file I receive the following error:

Example of error message

However note, I can immediately run Get-AzResourceGroup and it returns the group as I see it in Azure Portal.

Until I added the backend it was creating resources correctly so I'm thinking this is a simple configuration issue but after reviewing all the docs don't see what I've got wrong.


Solution

  • Ok, operator error as I suspected.

    Running az login --tenant '...' and then az account set --subscription '...' resolved the problem. terraform init now works correctly.