使用变量条件时的地形计数错误

时间:2020-04-23 14:09:35

标签: terraform

运行从0.11.15升级的Terraform 0.12.24代码,并获得与资源计数有关的错误。 count语句中有条件,并且代码行和错误是:

75:   count                  = var.vpc_count == 0 || var.tgw_count == 0 ? 0 : var.vpc_count

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

这两个变量都是0或1的简单数字,它们都位于tfvars文件中并传递到模块中。

当计数包含简单变量和条件变量时,我已搜索但找不到与该错误相关的任何东西。

如何使它正常工作?

这是模块内部的代码

resource "aws_route" "private-1-tgw" {
  provider               = aws.acct
  count                  = "${var.vpc_count}" == 0 || "${var.tgw_count}" == 0 ? 0 : "${var.vpc_count}"
  route_table_id         = aws_route_table.private-1[0].id
  destination_cidr_block = "10.0.0.0/8"
  transit_gateway_id     = var.tgw_id
}

这是tfvars文件中的内容

hmi_vpc_count = 1
tgw_count = 0

这是模块调用

module "hmi-vpc" {
  source = "../../modules/hmi-vpc"

  providers = {
    aws.acct = aws.acct
  }

  aws_account                   = var.aws_account
  aws_region                    = var.aws_region
  management_sg_rules           = var.management_sg_rules
  tgw_count                     = module.transit-gateway.tgw_id == "" ? 0 : var.tgw_count
  tgw_id                        = module.transit-gateway.tgw_id
  tags                          = var.tags
  vpc_cidr                      = var.hmi_vpc_cidr
  vpc_count                     = var.hmi_vpc_count
  vpc_dhcp_count                = var.hmi_vpc_dhcp_count
  vpc_dhcp_domain_name          = var.hmi_vpc_dhcp_domain_name
  vpc_dhcp_dns_servers          = var.hmi_vpc_dhcp_dns_servers
  vpc_dhcp_netbios_name_servers = var.hmi_vpc_dhcp_netbios_name_servers
  vpc_dhcp_ntp_servers          = var.hmi_vpc_dhcp_ntp_servers
  vpc_sn_private_1a_cidr        = var.hmi_vpc_sn_private_1a_cidr
  vpc_sn_private_1b_cidr        = var.hmi_vpc_sn_private_1b_cidr
  vpc_sn_private_1c_cidr        = var.hmi_vpc_sn_private_1c_cidr
  vpc_sn_public_1a_cidr         = var.hmi_vpc_sn_public_1a_cidr
  vpc_sn_public_1b_cidr         = var.hmi_vpc_sn_public_1b_cidr
}

1 个答案:

答案 0 :(得分:0)

问题出在(module "hmi-vpc" { ... }中)

  tgw_count = module.transit-gateway.tgw_id == "" ? 0 : var.tgw_count

取决于module.transit-gateway的执行。要解决此问题,您应该考虑一种条件,该条件不取决于模块的输出,而可能取决于模块的输入变量。例如:

  tgw_count = var.tgw_disabled ? 0 : var.tgw_count

或者甚至基于提供的信息

  tgw_count = var.tgw_count

可能会为您工作。