代码之家  ›  专栏  ›  技术社区  ›  iGEL

地形变量和计数=0

  •  3
  • iGEL  · 技术社区  · 7 年前

    我们在所有环境中使用相同的地形定义。到目前为止效果不错,但现在我面临一个我还无法解决的问题。我有一个RDS和ElastiCache,用于我现在正在设置的demo env中不需要的服务,所以我设置了 count 0 . 对于其他环境,我需要通过输出变量公开它们:

    resource "aws_elasticache_cluster" "cc_redis" {
      cluster_id = "cc-${var.env}"
      engine = "redis"
      node_type = "cache.t2.small"
      security_group_ids = ["..."]
      count = "${var.env == "demo" ? 0 : 1}"
    }
    
    output "cc_redis_host" {
      value = "${aws_elasticache_cluster.cc_redis.cache_nodes.0.address}"
    }
    

    现在我得到这个错误:

    output.cc_redis_host: Resource 'aws_elasticache_cluster.cc_redis' not found
    for variable 'aws_elasticache_cluster.cc_redis.cache_nodes.0.address'
    

    我不介意有一个无用的变量集,但我不能让它在一开始就工作。一个简单的条件不能解决这个问题,因为terraform计算条件的假边,即使它没有被使用。我发现 this hack 但也没能成功。

    2 回复  |  直到 7 年前
        1
  •  3
  •   iGEL    7 年前

    试试这个:

    output "cc_redis" {
      value = "${element(concat(aws_elasticache_cluster.cc_redis.*.cache_nodes.0.address, list("")), 0)}"
    }
    

    TF似乎并不关心如果在链的上面使用通配符,那么计数可能是0。

    这可能会比您想要的输出更多,但是您可以从中解析出您需要的内容。

        2
  •  0
  •   Adil B Cleve Green    7 年前

    将输出更改为以下内容:

    output "cc_redis_host" {
      value = "${element(concat(aws_elasticache_cluster.cc_redis.cache_nodes.*. address, list("")), 0)}"
    }
    

    它记录在terraform网站的某个地方。