Terraform AWS – How to Terraform ElastiCache Redis Cluster Provisioning Properly

amazon-elasticacheamazon-web-servicesterraform

I am currently writing Terraform script to provision ElastiCache Redis Cluster. I have the following concern. In the script, I use a snapshot name to restore database from ElastiCache single Redis instance.

I can spin up a new Redis cluster, however, when I try to add more shards (cluster_mode) to it from the tf script, it requires me to enter a proper snapshot name again (I have automatic backup, that is why snapshot name changes frequently). If it doesn't match, Terraform suggests me to destroy existing cluster and create a new one again.

resource "aws_elasticache_replication_group" "default" {
  replication_group_id          = "${var.cluster_id}"
  replication_group_description = "Redis cluster for Hashicorp ElastiCache example"

  node_type            = "cache.m4.large"
  port                 = 6379
  parameter_group_name = "default.redis3.2.cluster.on"

  snapshot_name = "${var.snapshot_name}"
  snapshot_retention_limit = 5
  snapshot_window          = "00:00-05:00"

  subnet_group_name = "${aws_elasticache_subnet_group.default.name}"

  automatic_failover_enabled = true

  cluster_mode {
    replicas_per_node_group = 1
    num_node_groups         = "${var.node_groups}"
  }
}   

Is it possible to split configuration of cluster creation and data restore into two configurations somehow?

Or, skip asking me a snapshot name if the cluster already provisioned?

Can I make this field optional?

Current actions:

  1. Create Redis cluster with specified snapshot;
  2. Add more shards (no snapshot name is asked therefore no cluster destroy, just configuration change).

Thanks.

Best Answer

So, this problem I solved with a lifecycle ignore_changes meta-argument:

lifecycle {
  ignore_changes = [
    "node_type",
    "snapshot_name",
  ]
}

I added snapshot_name resource attribute to ignore_changes meta-argument, and keep it there until I need to re-sotre cluster from the backup. In this case, I exclude it from the list.