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

Issue when creating module without a security group #134

Closed rafaljanicki closed 2 years ago

rafaljanicki commented 2 years ago

Describe the Bug

When using the module with associated_security_group_ids and disabled creation of security group, it throws an error about a null value:

╷
│ Error: Null value found in list
│ 
│   with module.elasticache-redis.aws_elasticache_replication_group.default[0],
│   on .terraform/modules/elasticache-redis/main.tf line 120, in resource "aws_elasticache_replication_group" "default":
│  120:   security_group_ids         = concat(local.associated_security_group_ids, [module.aws_security_group.id])
│ 
│ Null values are not allowed for this attribute value.

Expected Behavior

Should work correctly and add only the associated security groups

Steps to Reproduce

module "elasticache-redis" {
  source = "cloudposse/elasticache-redis/aws"
  version = "~> 0.41"

  name = "redis"
  subnets = var.private_subnets
  vpc_id = var.vpc_id

  associated_security_group_ids = [module.redis_sg.this_security_group_id]
  at_rest_encryption_enabled = true
  create_security_group = false
  kms_key_id = aws_kms_key.redis_key.arn
  instance_type = var.redis_instance_type
  transit_encryption_enabled = false
  zone_id = var.zone_id
}
nitrocode commented 2 years ago

Hmmm so since var.create_security_group = false and var.use_existing_security_groups == null, then local.create_security_group is false meaning that module.aws_security_group is disabled and so this results in the following expression

  security_group_ids         = concat(local.associated_security_group_ids, [null])

The issue would probably be resolved if we added compact

  security_group_ids         = compact(concat(local.associated_security_group_ids, [module.aws_security_group.id]))

cc: @Nuru would this be the appropriate fix for this ?

Nuru commented 2 years ago

The issue would probably be resolved if we added compact

  security_group_ids         = compact(concat(local.associated_security_group_ids, [module.aws_security_group.id]))

cc: @Nuru would this be the appropriate fix for this ?

Cannot use compact(). See #121