I use this Terraform example to create an ElastiCache Redis cluster (clustered mode enabled):
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
}
}
But how do I specify availability nodes for clusters and replicas? It’s possible via AWS console. I was hoping to add availability_zones = ["us-east-1a", "us-east-1c"]
to specify that all master nodes must be in us-east-1a and all replicas in us-east-1c, but got
Error creating Elasticache Replication Group: InvalidParameterCombination: PreferredCacheClusterAZs can only be specified for one node group.
I use Terraform v0.12.17 and aws provider v2.34.0.
2
Answers
It seems to me that this is impossible with the current Terraform aws provider (https://github.com/terraform-providers/terraform-provider-aws/issues/5104)
However, I found a useful workaround (it does not allow setting arbitrary AZ for each particular node as AWS console does, but it covers the most common use case): if you specify VPC subnets for your replication group via
subnet_group_name
key, cache instances will be created in the AZs of those subnets (order of subnets in a subnet group matters).Example Terraform config:
Result: I got a 6-shard cluster with all primary nodes in AZ of a subnet-123 and all replicas in AZ of a subnet-456. I have not tested it with more than one replica per node group.
Adding description to the problem as mentioned here.