我使用此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。
在我看来,当前的Terraform aws提供程序(https://github.com/terraform-providers/terraform-provider-aws/issues/5104)无法做到这一点
但是,我发现了一个有用的解决方法(它不允许像AWS控制台那样为每个特定节点设置任意AZ,但是它涵盖了最常见的用例):如果您通过subnet_group_name
键为复制组指定VPC子网,则将在这些子网的可用区(子网组matters中子网的order)中创建缓存实例。
示例地形配置:
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"
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中。我尚未针对每个节点组使用多个副本进行测试。