I have set up a Redis cluster with Terraform. The setup looks like this:

resource "aws_elasticache_replication_group" "instance" {
  replication_group_id = "test"

  node_type                     = "cache.t2.micro"
  port                          = 6379
  parameter_group_name          = "default.redis3.2.cluster.on"
  subnet_group_name             = "${aws_elasticache_subnet_group.instance.name}"
  security_group_ids            = ["${aws_security_group.instance.id}"]

  cluster_mode {
    replicas_per_node_group = 0
    num_node_groups         = "${var.cluster_size}"

  automatic_failover_enabled = true
  apply_immediately = true

When I change var.cluster_size from 2 to 3, I would have expected that Terraform will perform the update in place. However, that is not the case:

-/+ module.groupsign_redis.aws_elasticache_replication_group.instance (new resource required)
  id:                                              "test" => <computed> (forces new resource)
  apply_immediately:                               "true" => "true"
  at_rest_encryption_enabled:                      "false" => "false"
  auto_minor_version_upgrade:                      "true" => "true"
  automatic_failover_enabled:                      "true" => "true"
  cluster_mode.#:                                  "1" => "1"
  cluster_mode.3760271746.num_node_groups:         "" => "3" (forces new resource)
  cluster_mode.3760271746.replicas_per_node_group: "" => "0" (forces new resource)
  cluster_mode.3784625311.num_node_groups:         "2" => "0" (forces new resource)
  cluster_mode.3784625311.replicas_per_node_group: "0" => "0"

This is also confirmed in the documentation:

num_node_groups - (Required) Specify the number of node groups (shards) for this Redis replication group. Changing this number will force a new resource.

Currently, I see only the option to use Terraform for the initial deployment and then add or remove shards using the AWS UI, which will perform the update in place.

Do you know of a way to execute the in-place update with Terraform? Or is adding shards without loosing the state of the database simply not possible with Terraform at the moment?

Maybe it is possible to add/removing shards by hand (via the AWS UI) and then import the new state back into Terraform. I tried to run terraform refresh, but it does succeed in fully synchronizing the state. terraform apply still believes the resource has to be changed.

Update: What does work though is to simply ignore the change. Updating other Terraform resources still works. However, it is a bit confusing as the cluster size that is specified in the Terraform file does not match the actual size that is deployed. You then loose the ability to look at the Terraform setup and see exactly what is currently deployed.

Currently, that is the best option that I am ware of.


You can create aws_elasticache_replication_group resource and use data source aws_elasticache_cluster to retrieve an actual amount of nodes. This way you should not write ignore_changes.


Fortunately, the situation has improved and the problem no longer exists.

Terraform supports in-place updates of the number of shards now. To quote from the updated documentation:

num_node_groups Specify the number of node groups (shards) for this Redis replication group. Changing this number will trigger an online resizing operation before other settings modifications.

(Before, it ended with Changing this number will force a new resource.)

Your Answer

