Subscription could not be found – Terraform

huangapple go评论67阅读模式
英文:

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  = &quot;hashicorp/azurerm&quot;
          version = &quot;~&gt; 3.27.0&quot;
        }
      }
      backend &quot;azurerm&quot; {
        subscription_id      = &quot;&lt;Subscription A GUID&gt;&quot;                
        tenant_id            = &quot;&lt;Tenant GUID&gt;&quot;                
        resource_group_name  = &quot;rg-demo&quot;
        storage_account_name = &quot;storagename&quot;                                    
        container_name       = &quot;tfstate&quot;                                             
        key                  = &quot;env/state.tfstate&quot;
      }
    
    }
    
    provider &quot;azurerm&quot; {
      features {}
      
      subscription_id = &lt;Subscription B GUID&gt;
      tenant_id       = &lt;Tenant GUID&gt;
    }

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 &quot;azurerm&quot; {
  subscription_id = &quot;sub2&quot;
  tenant_id              = &quot;tenant1&quot;
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }

  }
terraform {
  backend &quot;azurerm&quot; {
     subscription_id      = &quot;sub2&quot;                
        tenant_id            = &quot;tenant1&quot; 
     resource_group_name  = &quot;myrg&quot;
     storage_account_name = &quot;remotestatekavstr233&quot;
     container_name       = &quot;terraform&quot;
     key                  = &quot;terraform.tfstate&quot;
  }
}

With configuring above ,got the error:

Error retrieving keys for Storage Account &quot;remotestatekavstr233&quot;: storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error

Subscription could not be found – Terraform

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 &quot;sub1 subscription Id&quot;

Subscription could not be found – Terraform

main.tf:

data &quot;azurerm_client_config&quot; &quot;current&quot; {
}

data azurerm_subscription &quot;current&quot;{ 
}
provider &quot;azurerm&quot; {
  subscription_id = &quot;sub2&quot;
  tenant_id              = &quot;tenant1&quot;
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }

  }


terraform {
  backend &quot;azurerm&quot; {
     subscription_id      = &quot;sub1&quot;                
        tenant_id            = &quot;tenant1&quot; 
        resource_group_name  = &quot;rg&quot;
      storage_account_name = &quot;remotestatekavstr233&quot;
     container_name       = &quot;terraform&quot;
      key                  = &quot;terraform.tfstate&quot;
  }
}

Then the terraform can be initialized successfully:

Subscription could not be found – Terraform

huangapple
  • 本文由 发表于 2023年5月11日 20:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定