英文:
Subscription could not be found - Terraform
问题
I'm getting the following error when trying to run terraform init using Az CLI in Azure GCC High. I am trying to store my tfstate in another subscription and deploy resource into another - both subs are in the same tenant. I've had success with the below terraform{} provider{} in commercial so I'm stumped!
> Initializing the backend... ╷ │ Error: Failed to get existing workspaces: Error retrieving keys for Storage Account "storageaccount": storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="SubscriptionNotFound" Message="The subscription '<Subscription A GUID>' could not be found."
I've cleared the .azure folder and switched to USGovernment using az cloud set --name AzureUSGovernment then az login. When az login completes, I see all the subscriptions I have oadmin to and <Subscription A> & <Subscription B> are visible and enabled in the output.
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.27.0"
    }
  }
  backend "azurerm" {
    subscription_id      = "<Subscription A GUID>"
    tenant_id            = "<Tenant GUID>"
    resource_group_name  = "rg-demo"
    storage_account_name = "storagename"                                  
    container_name       = "tfstate"                                             
    key                  = "env/state.tfstate"
  }
}
provider "azurerm" {
  features {}
  
  subscription_id = "<Subscription B GUID>"
  tenant_id       = "<Tenant GUID>"
}
Thanks in advance,
Brad.
英文:
I'm getting the following error when trying to run terraform init using Az CLI in Azure GCC High. I am trying to store my tfstate in another subscription and deploy resource into another - both subs are in the same tenant. I've had success with the below terraform{} provider{} in commercial so I'm stumped!
> Initializing the backend... ╷ │ Error: Failed to get existing
> workspaces: Error retrieving keys for Storage Account
> "storageaccount": storage.AccountsClient#ListKeys: Failure responding
> to request: StatusCode=404 -- Original Error: autorest/azure: Service
> returned an error. Status=404 Code="SubscriptionNotFound" Message="The
> subscription '<Subscription A GUID>' could not be found."
I've cleared the .azure folder and switched to USGovernment using az cloud set --name AzureUSGovernment then az login. When az login completes, I see all the subscriptions I have oadmin to and <Subscription A> & <Subscription B> are visible and enabled in the output.
  terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 3.27.0"
        }
      }
      backend "azurerm" {
        subscription_id      = "<Subscription A GUID>"                
        tenant_id            = "<Tenant GUID>"                
        resource_group_name  = "rg-demo"
        storage_account_name = "storagename"                                    
        container_name       = "tfstate"                                             
        key                  = "env/state.tfstate"
      }
    
    }
    
    provider "azurerm" {
      features {}
      
      subscription_id = <Subscription B GUID>
      tenant_id       = <Tenant GUID>
    }
Thanks in advance,
Brad.
答案1
得分: 0
请检查以下内容。
我尝试运行以下代码,而不设置当前订阅:
main.tf:
provider "azurerm" {
  subscription_id = "sub2"
  tenant_id      = "tenant1"
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
}
terraform {
  backend "azurerm" {
    subscription_id      = "sub2"
    tenant_id            = "tenant1"
    resource_group_name  = "myrg"
    storage_account_name = "remotestatekavstr233"
    container_name       = "terraform"
    key                  = "terraform.tfstate"
  }
}
在上述配置的基础上,遇到了以下错误:
Error retrieving keys for Storage Account "remotestatekavstr233": storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error
无论后端使用哪个订阅,或者您当前正在尝试使用哪个订阅,请执行以下命令来设置所有可用订阅中的当前订阅。并确保订阅处于启用状态而不是禁用状态。
运行以下命令来设置当前订阅:
az account set --subscription "sub1 subscription Id"
然后terraform可以成功初始化。
英文:
Check the following.
I tried to run the following without setting the current subscription:
main.tf:
provider "azurerm" {
  subscription_id = "sub2"
  tenant_id              = "tenant1"
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
terraform {
  backend "azurerm" {
     subscription_id      = "sub2"                
        tenant_id            = "tenant1" 
     resource_group_name  = "myrg"
     storage_account_name = "remotestatekavstr233"
     container_name       = "terraform"
     key                  = "terraform.tfstate"
  }
}
With configuring above ,got the error:
Error retrieving keys for Storage Account "remotestatekavstr233": storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error
Whatever subscription is kept for backend or you are currently trying to work with, execute the following command , to set the current subscription out of all the available ones.And make sure the subscription is enabled and not in disabled state.
run the following to set current subscription:
az account set --subscription "sub1 subscription Id"
main.tf:
data "azurerm_client_config" "current" {
}
data azurerm_subscription "current"{ 
}
provider "azurerm" {
  subscription_id = "sub2"
  tenant_id              = "tenant1"
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
terraform {
  backend "azurerm" {
     subscription_id      = "sub1"                
        tenant_id            = "tenant1" 
        resource_group_name  = "rg"
      storage_account_name = "remotestatekavstr233"
     container_name       = "terraform"
      key                  = "terraform.tfstate"
  }
}
Then the terraform can be initialized successfully:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论