cloudposse / terraform-aws-elasticache-redis

Terraform module to provision an ElastiCache Redis Cluster
https://cloudposse.com/accelerate
Apache License 2.0
141 stars 244 forks source link

Using default value for availability_zones break Terraform #63

Closed the-smooth-operator closed 2 years ago

the-smooth-operator commented 4 years ago

Describe the Bug

When using the default value for availability_zones (i.e: the parameter it's omitted) terraform plan throws the next error:


  on .terraform/modules/redis-dev/main.tf line 91, in resource "aws_elasticache_replication_group" "default":
  91:   availability_zones            = slice(var.availability_zones, 0, var.cluster_size)
    |----------------
    | var.cluster_size is 3

Invalid value for "end_index" parameter: end index must not be greater than
the length of the list.

The problem here is that slice() tries to create a non empty slice of an empty list, thus failing. More information on the slice command here.

Expected Behavior

terraform plan runs.

Steps to Reproduce

Create a file with this content:

  module "redis-dev" {
    source                     = "git::https://github.com/cloudposse/terraform-aws-elasticache-redis.git?ref=tags/0.16.0"
    stage                       = "dev"
    name                       = "voice-eks-dev"
    vpc_id                     = module.vpc.vpc_id
    subnets                   = module.vpc.private_subnets.0
    cluster_size             = 1
    instance_type          = "cache.t3.micro"
    engine_version        = "5.0.0"
    family                       = "redis5.0"
}

Environment (please complete the following information):

Anything that will help us triage the bug will help. Here are some ideas:

hawkesn commented 3 years ago

Are there plans to merge in the fix that @sharath-sequoia mentioned? I'm running into the same issue.

gusse commented 3 years ago

slice() function also fails if you want to create more nodes than you have defined availability zones. Which is kind of the same as was what reported originally in this issue..

module "redis_test" {
  source             = "git::https://github.com/cloudposse/terraform-aws-elasticache-redis.git?ref=tags/0.31.0"
  name               = "redis_test"
  vpc_id             = module.vpc.vpc_id
  subnets            = ["subnet-123456", "subnet-789012"]
  cluster_size       = 4
  instance_type      = "cache.t3.micro"
  availability_zones = ["us-east-1a", "us-east-1b"]
}

terraform plan

Error: Invalid function argument

  on .terraform/modules/redis_test/main.tf line 89, in resource "aws_elasticache_replication_group" "default":
  89:   availability_zones            = var.cluster_mode_enabled ? null : slice(var.availability_zones, 0, var.cluster_size)
    |----------------
    | var.cluster_size is 4

Invalid value for "end_index" parameter: end index must not be greater than
the length of the list.

I noticed this earlier with the memcached module and made a PR for it. I'll make a PR for what @sharath-sequoia had done already and my fix for the amount of nodes.

gusse commented 3 years ago

Unfortunately defining more nodes than AZs will require a fix for AWS provider as well as there seems to be a bug that prevents you from defining duplicate values for availability_zones parameter on aws_elasticache_replication_groupresource, https://github.com/hashicorp/terraform-provider-aws/issues/14070

I'll do the PR anyways, but that bug explains why I was losing my mind testing the change and couldn't understand why it didn't produce a longer list of AZs 😄

Nuru commented 2 years ago

Closed by #108