<p>Fortunately, the situation has improved and the problem no longer exists.</p>

<p>Terraform supports in-place updates of the number of shards now. To quote from the updated <a href="https://www.terraform.io/docs/providers/aws/r/elasticache_replication_group.html#num_node_groups" rel="nofollow noreferrer">documentation</a>:</p>

<blockquote>
  <p><code>num_node_groups</code> 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.</p>
</blockquote>

<p>(Before, it ended with <em>Changing this number will force a new resource.</em>)</p>

CC BY-SA 4.0
2019-07-25T23:48:30.217Z
2019-07-25T23:48:30.217Z
Philipp Claßen
<p>You can create <code>aws_elasticache_replication_group</code> resource and use data source <a href="https://www.terraform.io/docs/providers/aws/d/elasticache_cluster.html" rel="nofollow noreferrer">aws_elasticache_cluster</a> to retrieve an actual amount of nodes. This way you should not write <code>ignore_changes</code>.</p>

CC BY-SA 3.0
2018-03-08T18:03:09.267Z
2018-03-08T18:03:09.267Z
Anton Babenko
<p>I have set up a Redis cluster with Terraform. The setup looks like this:</p>

<pre><code>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
}
</code></pre>

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

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

<p>This is also confirmed in the <a href="https://www.terraform.io/docs/providers/aws/r/elasticache_replication_group.html" rel="noreferrer">documentation</a>:</p>

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

<p>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.</p>

<p>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?</p>

<p>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 <code>terraform refresh</code>, but it does succeed in fully synchronizing the state. <code>terraform apply</code> still believes the resource has to be changed.</p>

<p><strong>Update:</strong>
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.</p>

<p>Currently, that is the best option that I am ware of.</p>

<amazon-web-services><terraform><redis>
How to add shards to a Redis cluster managed by Terraform without loosing all state?
Philipp Claßen
# How to add shards to a Redis cluster managed by Terraform without loosing all state?

(Asked by: username_0 on 2018-03-08)

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" =>  (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.

**Tags:** amazon-web-services, terraform, redis


---

## 2 answers

### Answer by username_0 on 2019-07-25. Score: 1 | Accepted answer

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](https://www.terraform.io/docs/providers/aws/r/elasticache_replication_group.html#num_node_groups):

> `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.*)


---

### Answer by username_1 on 2018-03-08. Score: 1

You can create `aws_elasticache_replication_group` resource and use data source [aws\_elasticache\_cluster](https://www.terraform.io/docs/providers/aws/d/elasticache_cluster.html) to retrieve an actual amount of nodes. This way you should not write `ignore_changes`.


---

