Terraform计划因数据元素错误地声称不存在而失败

时间:2019-12-02 21:17:58

标签: azure terraform

我正在Mac OS上运行Terraform v0.12.16。我有一个terraform文件,它引用了Azure的数据查询:

provider "azurerm" {
  version = "=1.37.0"
}

terraform {
  backend "azurerm" {
    storage_account_name = "tfstate"
    container_name       = "tfstate"
    key                  = "terraform.tfstate"
  }
}

/* Pull in shared resources */
data "azurerm_resource_group" "dev" {
  name = "Development"
}

data "azurerm_resource_group" "uat" {
  name = "UAT"
}

data "azurerm_resource_group" "staging" {
  name = "Staging"
}

data "azurerm_resource_group" "prod" {
  name = "Production"
}

data "azurerm_container_registry" "acr" {
  name                = "containerRegistryAU"
  resource_group_name = data.azurerm_resource_group.dev.name
}

运行地形计划时,我得到:

terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

random_integer.ri: Refreshing state... [id=44293]
data.azurerm_dns_zone.staging_ynomia_com: Refreshing state...
data.azurerm_resource_group.dev: Refreshing state...
data.azurerm_resource_group.staging: Refreshing state...
data.azurerm_dns_zone.uat_ynomia_com: Refreshing state...
data.azurerm_resource_group.prod: Refreshing state...
data.azurerm_resource_group.uat: Refreshing state...
data.azurerm_dns_zone.dev_ynomia_com: Refreshing state...
data.azurerm_dns_zone.ynomia_com: Refreshing state...

Error: Error: Resource Group "Development" was not found

  on _main.tf line 27, in data "azurerm_resource_group" "dev":
  27: data "azurerm_resource_group" "dev" {



Error: Error: Resource Group "UAT" was not found

  on _main.tf line 31, in data "azurerm_resource_group" "uat":
  31: data "azurerm_resource_group" "uat" {



Error: Error: Resource Group "Staging" was not found

  on _main.tf line 35, in data "azurerm_resource_group" "staging":
  35: data "azurerm_resource_group" "staging" {



Error: Error: Resource Group "Production" was not found

  on _main.tf line 39, in data "azurerm_resource_group" "prod":
  39: data "azurerm_resource_group" "prod" {



Error: Error: DNS Zone "ynomia.com" was not found

  on _main.tf line 138, in data "azurerm_dns_zone" "ynomia_com":
 138: data "azurerm_dns_zone" "ynomia_com" {



Error: Error: DNS Zone "dev.ynomia.com" was not found

  on _main.tf line 142, in data "azurerm_dns_zone" "dev_ynomia_com":
 142: data "azurerm_dns_zone" "dev_ynomia_com" {



Error: Error: DNS Zone "uat.ynomia.com" was not found

  on _main.tf line 146, in data "azurerm_dns_zone" "uat_ynomia_com":
 146: data "azurerm_dns_zone" "uat_ynomia_com" {



Error: Error: DNS Zone "staging.ynomia.com" was not found

  on _main.tf line 150, in data "azurerm_dns_zone" "staging_ynomia_com":
 150: data "azurerm_dns_zone" "staging_ynomia_com" {

但是我知道这些资源存在: Azure portal showing my resource groups

这已经工作了几个月了,所以我为为什么突然失败感到困惑。我该如何调试这里发生的事情?

1 个答案:

答案 0 :(得分:1)

我知道,可能的原因是您使用的是本地Azure CLI当前使用的订阅,但它不是您要使用的正确订阅。

因此,您需要找到资源所属的正确订阅。然后像这样更改Terraform提供程序:

provider "azurerm" {
  version = "=1.37.0"

  subscription_id = "your_subscription_id"
  tenant_id = "your_tenant_id"
}

它将从您需要使用的订阅和租户那里获取资源组。