Skip to content

Sre 8582 redis7 support #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cloudwatch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
alarm_actions = ["${var.alarm_actions}"]
}
*/

75 changes: 53 additions & 22 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,44 +1,75 @@
# 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" {
byte_length = 8
}

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
}
}

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
}
13 changes: 7 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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"
}
28 changes: 14 additions & 14 deletions security_groups.tf
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 25 additions & 10 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
Expand Down Expand Up @@ -57,34 +57,37 @@ 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" {
description = "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" {
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}