【问题标题】:Conditionally create resources with terraform使用 terraform 有条件地创建资源
【发布时间】:2017-06-08 18:28:06
【问题描述】:

我正在尝试编写一个模块来在 AWS 上部署 Sensu,计划是为 Redis 使用 elasticache,但现在我面临一个边缘案例。我们的一些 VPC 的租户设置为专用,我们不能在那里使用 elasticache (http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/AmazonVPC.EC.html),我想我可以通过有条件地使用 elasticache 或在 ASG 中部署一个 redis 集群来解决这个问题。我已经编写了代码并使用了:

count = "${replace(replace(var.vpc_instance_tenancy,"/.*dedicated.*/","0"),"/(.*default.*|^$)/","1")}"

要决定我应该创建 elasticahe 集群还是 ASG,问题是我需要将主机名或 IP 地址传递给 Sensu 服务器和 api 节点,以便它们可以连接到 Redis,计划使用:

redis_host = "${coalesce(aws_elasticache_cluster.redis_cluster.cache_nodes.0.address,aws_elb.redis_lb.dns_name)}"

但这总是失败,因为其中一个资源永远不会被创建,我无法在 coalesce 函数中引用它。有什么想法吗?

【问题讨论】:

    标签: amazon-web-services terraform amazon-elasticache


    【解决方案1】:

    我设法让它工作,看起来如果我们使用 splat 变量格式,我们可以引用不存在的资源,如下所示:

    redis_host = "${element(concat(aws_elasticache_cluster.redis_cluster.*.cache_nodes.0.address, aws_elb.redis_lb.*.dns_name), 0)}"
    redis_port = "${element(concat(aws_elasticache_cluster.redis_cluster.*.cache_nodes.0.port, list(var.redis_port)), 0)}"
    

    所以不存在的资源将返回一个空列表,而另一个将返回单个元素列表,我将它们连接在一起并获取第一个元素。

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 2017-12-09
      • 2020-05-30
      • 2021-12-21
      • 1970-01-01
      • 2021-11-20
      • 2021-01-21
      • 2018-01-21
      相关资源
      最近更新 更多