HiveBrain v1.2.0
Get Started
← Back to all entries
snippetterraformMinor

How to add shards to a Redis cluster managed by Terraform without loosing all state?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
redismanagedwithoutallterraformshardsloosingstatehowadd

Problem

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

Solution

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.

Context

StackExchange DevOps Q#3575, answer score: 1

Revisions (0)

No revisions yet.