diff --git a/cloudwatch.tf b/cloudwatch.tf index 75bbea9..5ec7a4d 100644 --- a/cloudwatch.tf +++ b/cloudwatch.tf @@ -45,4 +45,3 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" { alarm_actions = ["${var.alarm_actions}"] } */ - diff --git a/main.tf b/main.tf index 40ebd3c..68d5521 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,12 @@ +# Added support for Redis 7. You need to use redis_version value as below for different version +# Redis < 6.x, need to enter redis_version value like 3.2.1 Means Major.Minor.patch +# Redis >= 6.x need to enter redis_version value like 6.x Means Major.x +locals { + parameter_group_family = substr(var.redis_version, 0,1) < 6 ? "redis${replace(var.redis_version, "/\\.[\\d]+$/", "")}": (substr(var.redis_version, 0,1) == "6" ? "redis${substr(var.redis_version, 0,1)}.x": "redis${substr(var.redis_version, 0,1)}") +} + data "aws_vpc" "vpc" { - id = "${var.vpc_id}" + id = var.vpc_id } resource "random_id" "salt" { @@ -7,31 +14,55 @@ resource "random_id" "salt" { } resource "aws_elasticache_replication_group" "redis" { - replication_group_id = "${format("%.20s","${var.name}-${var.env}")}" - replication_group_description = "Terraform-managed ElastiCache replication group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}" - number_cache_clusters = "${var.redis_clusters}" - node_type = "${var.redis_node_type}" - automatic_failover_enabled = "${var.redis_failover}" - engine_version = "${var.redis_version}" - port = "${var.redis_port}" - parameter_group_name = "${aws_elasticache_parameter_group.redis_parameter_group.id}" - subnet_group_name = "${aws_elasticache_subnet_group.redis_subnet_group.id}" - security_group_ids = ["${aws_security_group.redis_security_group.id}"] - apply_immediately = "${var.apply_immediately}" - maintenance_window = "${var.redis_maintenance_window}" - snapshot_window = "${var.redis_snapshot_window}" - snapshot_retention_limit = "${var.redis_snapshot_retention_limit}" - tags = "${merge(map("Name", format("tf-elasticache-%s-%s", var.name, lookup(data.aws_vpc.vpc.tags,"Name",""))), var.tags)}" + replication_group_id = replace(format("%.20s", "${var.name}-${var.env}"), "/-$/", "") + description = "Terraform-managed ElastiCache replication group for ${var.name}-${var.env}" + num_cache_clusters = var.redis_clusters + node_type = var.redis_node_type + automatic_failover_enabled = var.redis_failover + engine_version = var.redis_version + port = var.redis_port + parameter_group_name = aws_elasticache_parameter_group.redis_parameter_group.id + subnet_group_name = aws_elasticache_subnet_group.redis_subnet_group.id + security_group_ids = [aws_security_group.redis_security_group.id] + apply_immediately = var.apply_immediately + maintenance_window = var.redis_maintenance_window + snapshot_window = var.redis_snapshot_window + snapshot_retention_limit = var.redis_snapshot_retention_limit + security_group_names = [] # This is needed to fix bug in AWS provider 5.30.0 - see https://github.com/hashicorp/terraform-provider-aws/issues/32835 + preferred_cache_cluster_azs = var.availability_zones + tags = merge( + { + "Name" = format( + "tf-elasticache-%s-%s", + var.name, + lookup(data.aws_vpc.vpc.tags, "Name", ""), + ) + }, + var.tags, + ) } resource "aws_elasticache_parameter_group" "redis_parameter_group" { - name = "${replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}-${random_id.salt.hex}", "_", "-"))), "/\\s/", "-")}" - description = "Terraform-managed ElastiCache parameter group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}" + # tf-redis-sc-api-queue-dev + name = "tf-redis-${var.name}-${var.env}" + + description = "Terraform-managed ElastiCache parameter group for ${var.name}-${var.env}" # Strip the patch version from redis_version var - family = "redis${replace(var.redis_version, "/\\.[\\d]+$/","")}" - parameter = "${var.redis_parameters}" + family = local.parameter_group_family + dynamic "parameter" { + for_each = var.redis_parameters + content { + # TF-UPGRADE-TODO: The automatic upgrade tool can't predict + # which keys might be set in maps assigned here, so it has + # produced a comprehensive set here. Consider simplifying + # this after confirming which keys can be set in practice. + + name = parameter.value.name + value = parameter.value.value + } + } lifecycle { create_before_destroy = true @@ -39,6 +70,6 @@ resource "aws_elasticache_parameter_group" "redis_parameter_group" { } resource "aws_elasticache_subnet_group" "redis_subnet_group" { - name = "${replace(format("%.255s", lower(replace("tf-redis-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}", "_", "-"))), "/\\s/", "-")}" - subnet_ids = ["${var.subnets}"] + name = "tf-redis-${var.name}-${var.env}" + subnet_ids = var.subnets } diff --git a/outputs.tf b/outputs.tf index da6df54..462e67b 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,23 +1,24 @@ output "redis_security_group_id" { - value = "${aws_security_group.redis_security_group.id}" + value = aws_security_group.redis_security_group.id } output "parameter_group" { - value = "${aws_elasticache_parameter_group.redis_parameter_group.id}" + value = aws_elasticache_parameter_group.redis_parameter_group.id } output "redis_subnet_group_name" { - value = "${aws_elasticache_subnet_group.redis_subnet_group.name}" + value = aws_elasticache_subnet_group.redis_subnet_group.name } output "id" { - value = "${aws_elasticache_replication_group.redis.id}" + value = aws_elasticache_replication_group.redis.id } output "port" { - value = "${var.redis_port}" + value = var.redis_port } output "endpoint" { - value = "${aws_elasticache_replication_group.redis.primary_endpoint_address}" + value = var.cluster_mode_enabled ? join("", compact(aws_elasticache_replication_group.redis[*].configuration_endpoint_address)) : join("", compact(aws_elasticache_replication_group.redis[*].primary_endpoint_address)) + description = "Redis primary or configuration endpoint, whichever is appropriate for the given cluster mode" } diff --git a/security_groups.tf b/security_groups.tf index 9b86875..e4f6de5 100644 --- a/security_groups.tf +++ b/security_groups.tf @@ -1,28 +1,28 @@ resource "aws_security_group" "redis_security_group" { - name = "${format("%.255s", "tf-sg-ec-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}")}" - description = "Terraform-managed ElastiCache security group for ${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}" - vpc_id = "${data.aws_vpc.vpc.id}" + name = "tf-sg-ec-${var.name}-${var.env}" + description = "Terraform-managed ElastiCache security group for ${var.name}-${var.env}" + vpc_id = data.aws_vpc.vpc.id - tags { - Name = "tf-sg-ec-${var.name}-${var.env}-${data.aws_vpc.vpc.tags["Name"]}" + tags = { + Name = "tf-sg-ec-${var.name}-${var.env}" } } resource "aws_security_group_rule" "redis_ingress" { - count = "${length(var.allowed_security_groups)}" + count = length(var.allowed_security_groups) type = "ingress" - from_port = "${var.redis_port}" - to_port = "${var.redis_port}" + from_port = var.redis_port + to_port = var.redis_port protocol = "tcp" - source_security_group_id = "${element(var.allowed_security_groups, count.index)}" - security_group_id = "${aws_security_group.redis_security_group.id}" + source_security_group_id = element(var.allowed_security_groups, count.index) + security_group_id = aws_security_group.redis_security_group.id } resource "aws_security_group_rule" "redis_networks_ingress" { type = "ingress" - from_port = "${var.redis_port}" - to_port = "${var.redis_port}" + from_port = var.redis_port + to_port = var.redis_port protocol = "tcp" - cidr_blocks = ["${var.allowed_cidr}"] - security_group_id = "${aws_security_group.redis_security_group.id}" + cidr_blocks = var.allowed_cidr + security_group_id = aws_security_group.redis_security_group.id } diff --git a/variables.tf b/variables.tf index 9da1683..af38037 100644 --- a/variables.tf +++ b/variables.tf @@ -20,13 +20,13 @@ variable "apply_immediately" { } variable "allowed_cidr" { - type = "list" + type = list(string) default = ["127.0.0.1/32"] description = "A list of Security Group ID's to allow access to." } variable "allowed_security_groups" { - type = "list" + type = list(string) default = [] description = "A list of Security Group ID's to allow access to." } @@ -57,14 +57,14 @@ variable "redis_port" { } variable "subnets" { - type = "list" + type = list(string) description = "List of VPC Subnet IDs for the cache subnet group" } # might want a map variable "redis_version" { - description = "Redis version to use, defaults to 3.2.10" - default = "3.2.10" + description = "Redis version to use, defaults to 3.2.4" + default = "3.2.4" } variable "vpc_id" { @@ -72,19 +72,22 @@ variable "vpc_id" { } variable "redis_parameters" { - type = "list" - description = "additional parameters modifyed in parameter group" - default = [] + description = "additional parameters modified in parameter group" + type = list(object({ + name = string + value= string + })) + default = [] } variable "redis_maintenance_window" { description = "Specifies the weekly time range for when maintenance on the cache cluster is performed. The format is ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC). The minimum maintenance window is a 60 minute period" - default = "fri:08:00-fri:09:00" + default = "tue:23:30-wed:00:30" } variable "redis_snapshot_window" { description = "The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. The minimum snapshot window is a 60 minute period" - default = "06:30-07:30" + default = "04:00-05:00" } variable "redis_snapshot_retention_limit" { @@ -96,3 +99,15 @@ variable "tags" { description = "Tags for redis nodes" default = {} } + +variable "availability_zones" { + description = "A list of EC2 availability zones in which the replication group's cache clusters will be created. The order of the availability zones in the list is not important" + type = list(string) + default = [] +} + +variable "cluster_mode_enabled" { + type = bool + description = "Flag to enable/disable creation of a native redis cluster. `automatic_failover_enabled` must be set to `true`. Only 1 `cluster_mode` block is allowed" + default = false +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..ac97c6a --- /dev/null +++ b/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +}