我们在所有环境中使用相同的地形定义。到目前为止,效果很好,但现在我面临着一个尚未解决的问题。我有一个 RDS 和 ElastiCache 用于我现在正在设置的演示环境中不需要的服务,因此我将其设置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}"
}
Run Code Online (Sandbox Code Playgroud)
现在我收到此错误:
output.cc_redis_host: Resource 'aws_elasticache_cluster.cc_redis' not found
for variable 'aws_elasticache_cluster.cc_redis.cache_nodes.0.address'
Run Code Online (Sandbox Code Playgroud)
我不太介意设置一个无用的变量,但我一开始就无法让它工作。简单的条件并不能解决这个问题,因为 terraform 会评估条件的错误一面,即使它没有被使用。我发现了这个 hack,但也无法让它工作。
terraform ×1