Terraform:具有指定可用性区域的ElastiCache Redis群集?

时间:2019-12-05 11:53:44

标签: terraform amazon-elasticache

我使用以下Terraform示例创建ElastiCache Redis集群(启用集群模式): https://www.terraform.io/docs/providers/aws/r/elasticache_replication_group.html#redis-cluster-mode-enabled

resource "aws_elasticache_replication_group" "example" {
  replication_group_id = "example-group"
  engine_version = "5.0.5"
  node_type = "cache.r5.large"
  port = 6379
  automatic_failover_enabled = true

  cluster_mode {
    replicas_per_node_group = 1
    num_node_groups = 6
  }
}

但是如何为群集和副本指定可用性节点?可以通过AWS控制台。我希望添加availability_zones = ["us-east-1a", "us-east-1c"]来指定所有主节点必须在us-east-1a中,所有副本都在us-east-1c中,但是得到了 Error creating Elasticache Replication Group: InvalidParameterCombination: PreferredCacheClusterAZs can only be specified for one node group.

我使用Terraform v0.12.17和aws提供程序v2.34.0。

2 个答案:

答案 0 :(得分:0)

在我看来,当前的Terraform aws提供程序(https://github.com/terraform-providers/terraform-provider-aws/issues/5104)不可能做到这一点

但是,我发现了一个有用的解决方法(它不允许像AWS控制台那样为每个特定节点设置任意AZ,但是它涵盖了最常见的用例): 如果您通过subnet_group_name键为复制组指定VPC子网,则将在这些子网的可用区(子网组要点中子网的 order )中创建缓存实例。 >)。

Terraform配置示例:

resource "aws_elasticache_subnet_group" "redis_subnet_group" {
  name       = "example-subnet-group"
  subnet_ids = ["subnet-123", "subnet-456"]
}        

resource "aws_elasticache_replication_group" "redis_replication_group" {
  replication_group_id          = "example-replication-group"
  engine_version                = "5.0.5"
  node_type                     = "cache.r5.large"
  port                          = 6379
  automatic_failover_enabled    = true
  subnet_group_name             = aws_elasticache_subnet_group.redis_subnet_group.name

  cluster_mode {
    replicas_per_node_group = 1
    num_node_groups         = 6
  }
}

结果:我得到了一个6分片群集,其中所有主节点位于子网123的AZ中,而所有副本位于子网456的AZ中。我尚未针对每个节点组使用多个副本进行测试。

答案 1 :(得分:0)

按照here为问题添加描述。

发生这种情况的原因是因为{{1} 参数与启用Redis群集模式的复制不兼容 碎片多于1个的组。

在Elasticache SDK中,这是有关以下内容的完整文档: availability_zones设置的参数:

availability_zones

因此要为// A list of EC2 Availability Zones in which the replication group's clusters // are created. The order of the Availability Zones in the list is the order // in which clusters are allocated. The primary cluster is created in the first // AZ in the list. // // This parameter is not used if there is more than one node group (shard). // You should use NodeGroupConfiguration instead. // // If you are creating your replication group in an Amazon VPC (recommended), // you can only locate clusters in Availability Zones associated with the subnets // in the selected subnet group. // // The number of Availability Zones listed must equal the value of NumCacheClusters. // // Default: system chosen Availability Zones. PreferredCacheClusterAZs []*string `locationNameList:"AvailabilityZone" type:"list"` 明确配置相同的内容 Redis群集模式已禁用或多个可用区 单个分片复制组,该属性确实需要 与availability_zones的迁移方式类似 preferred_availability_zones资源。

对于启用Redis群集模式的复制组(例如,当使用 Terraform中的cluster_mode),我们目前尚无法 通过aws_elasticache_cluster参数设置可用区, 这可能需要更改NodeGroupConfiguration参数。