地形变量和计数= 0

时间:2018-08-02 13:26:47

标签: terraform

我们在所有环境中使用相同的地形定义。到目前为止,效果很好,但现在我遇到了无法解决的问题。我有一个RDS和ElastiCache用于我现在要设置的演示环境中不需要的服务,因此我将count设置为0。对于其他环境,我需要通过输出var公开它们:

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 个答案:

答案 0 :(得分:3)

尝试一下:

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

如果您在链中使用通配符,TF似乎并不在乎该计数可能为0。

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

答案 1 :(得分:0)

将输出更改为以下内容:

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

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