动态更改terraform中aws_elasticache_replication_group的配置

问题描述 投票:1回答:1

我正在使用terraform配置弹性缓存集群,一切正常!现在我的要求是我想在集群模式的资源内部进行动态配置。

以下是我的常用代码..

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个回答
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的值切换创建的资源。

© www.soinside.com 2019 - 2024. All rights reserved.