skip to Main Content

I am trying to do Terraform init and i am getting the below error

│ Error: Failed to get existing workspaces: Error retrieving keys for Storage Account "stmpltfstateprdcus001": storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceGroupNotFound" Message="Resource group 'rg-terraform-prod-centralus-001' could not be found."

This is my backend configuration :

terraform {
  required_version = "~> 1.0"


  required_providers {
    azuread = "~> 3.0"
    azurerm = "~> 4.0"
  }
  backend "azurerm" {
      resource_group_name  = "rg-terraform-prod-centralus-001"
      storage_account_name = "stmpltfstateprdcus001"
      container_name       = "terraform"
      key                  = "mg-PLATFORM-repo-Azure_Lz_Core-branch-main-template-platform_landing_zone.tfstate"
    }
}

I am logged in to Azure with my account and i can see the Storage Account Access Keys. I have full access

So what is the issue here

2

Answers


  1. Before running terraform init through your current account, you need to authorize in the same command line session:

    1. Install az command line: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli
    2. Run the following command to authorize and set the default session Azure subscription: az login

    Additionally, you may consider using the Shared access key (View account access keys, azurerm-Authentication)

    backend "azurerm" {
      resource_group_name  = "rg-terraform-prod-centralus-001"
      storage_account_name = "stmpltfstateprdcus001"
      container_name       = "terraform"
      key                  = "mg-PLATFORM-repo-Azure_Lz_Core-branch-main-template-platform_landing_zone.tfstate"
      access_key           = "..."
    }
    
    Login or Signup to reply.
  2. If you can see the Storage Account in Azure but Terraform is giving a 404 error for the resource group, then check:

    1. Verify the authenticated Azure subscription in your terminal: az account show

    2. If the subscription context is incorrect, list available subs and change the context

    az account list --output table
    az account set --subscription <sub name|ID here>"
    
    1. Validate Azure credentials are properly set up for Terraform by setting env. variables:
    export ARM_SUBSCRIPTION_ID="your-subscription-id"
    export ARM_TENANT_ID="your-tenant-id"
    export ARM_CLIENT_ID="your-client-id"
    export ARM_CLIENT_SECRET="your-client-secret"
    
    • Or if you’re using az cli authentication: export ARM_USE_CLI=true
    1. Verify the resource group exists in your current subscription: az group show --name rg-terraform-prod-centralus-001

    2. Check storage account accessibility: az storage account show --name stmpltfstateprdcus001 --resource-group rg-terraform-prod-centralus-001

    It’s likely that there is a mismatch between the subscription you’re authenticated to and the subscription containing the backend resources.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search