【问题标题】:Dynamically change in the configuration of aws_elasticache_replication_group in terraform在 terraform 中动态更改 aws_elasticache_replication_group 的配置
【发布时间】:2018-06-01 19:02:44
【问题描述】:

我正在使用 terraform 配置 elasticache 集群,一切正常! 现在我的要求是我想为 cluster-mode 做资源内部的动态配置。

下面是我的常用代码..

resource "aws_elasticache_replication_group" "elasticache_redis_cluster" {
  replication_group_id          = "cache"
  engine_version                = "${var.engine_version}"
  node_type                     = "${var.node_type}"
  port                          = "${var.elasticache_port}"
  parameter_group_name          = "${var.param_group_name}"
  security_group_ids            = ["${aws_sg.id}"]
  subnet_group_name             = "${aws_elasticache_subnet_group.subnet_group.id}"
}

现在我想根据传递的参数执行以下操作。

  if (${var.cluster_mode == "enable") {
        automatic_failover_enabled    = true  
        cluster_mode { 
           replicas_per_node_group     = 1 
           num_node_groups             = 1
        }
  }
  else {
        number_cache_clusters = 2
  }

以上基于匹配条件的代码应附加在集群的配置中。

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    Terraform Conditionals 只支持三元赋值。

    例如,它们只能采用以下形式:

    resource "cool_thing" "my_resource" {
        is_prod_thing = "${var.env == "production" ? true : false}"
    }
    

    三元运算返回的值必须是相同的类型,并且没有直接的方法可以在不同的资源配置之间进行内部切换。

    一种可能的解决方法是使用 count Meta-Parameter 根据变量值创建零个或多个资源:

    variable "cluster_mode" {
      default = "enable"
    }
    
    locals {
      cluster_count = "${var.cluster_mode == "enable" ? 1 : 0}"
      non_cluster_count = "${var.cluster_mode == "enable" ? 0 : 1}"
    } 
    
    resource "aws_elasticache_replication_group" "elasticache_redis_cluster" {
      # Configuration for clustered nodes
      count = "${local.cluster_count}"
    }
    
    resource "aws_elasticache_replication_group" "elasticache_redis_non_cluster" {
      # Configuration for non-clustered nodes
      count = "${local.non_cluster_count}"
    }
    

    通过这种方式,您可以描述可能需要的资源的两种配置,并根据cluster_mode 的值切换创建哪一种。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多