Skip to content
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

add cbd functionality to cache parameter group #201

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions main.tf
Expand Up @@ -78,6 +78,8 @@ locals {
)

elasticache_member_clusters = module.this.enabled ? tolist(aws_elasticache_replication_group.default[0].member_clusters) : []

parameter_group_name = join("", var.parameter_group_create_before_destroy ? aws_elasticache_parameter_group.cbd[*].name : aws_elasticache_parameter_group.default[*].name)
}

resource "aws_elasticache_subnet_group" "default" {
Expand All @@ -88,8 +90,41 @@ resource "aws_elasticache_subnet_group" "default" {
tags = module.this.tags
}

resource "random_id" "cache_version_change_forces_new_parameter_group" {
count = module.this.enabled && var.parameter_group_create_before_destroy ? 1 : 0
byte_length = 8
keepers = {
family = var.family
}
}

resource "aws_elasticache_parameter_group" "cbd" {
count = module.this.enabled && var.parameter_group_create_before_destroy ? 1 : 0
name = "${module.this.id}-${random_id.cache_version_change_forces_new_parameter_group[0].dec}"
description = var.parameter_group_description != null ? var.parameter_group_description : "Elasticache parameter group for ${module.this.id}"
family = var.family

dynamic "parameter" {
for_each = var.cluster_mode_enabled ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter
content {
name = parameter.value.name
value = tostring(parameter.value.value)
}
}

tags = module.this.tags

# Ignore changes to the description since it will try to recreate the resource
lifecycle {
create_before_destroy = true
ignore_changes = [
description,
]
}
}

resource "aws_elasticache_parameter_group" "default" {
count = module.this.enabled ? 1 : 0
count = module.this.enabled && !var.parameter_group_create_before_destroy ? 1 : 0
name = module.this.id
description = var.parameter_group_description != null ? var.parameter_group_description : "Elasticache parameter group for ${module.this.id}"
family = var.family
Expand Down Expand Up @@ -121,7 +156,7 @@ resource "aws_elasticache_replication_group" "default" {
node_type = var.instance_type
num_cache_clusters = var.cluster_mode_enabled ? null : var.cluster_size
port = var.port
parameter_group_name = join("", aws_elasticache_parameter_group.default[*].name)
parameter_group_name = local.parameter_group_name
preferred_cache_cluster_azs = length(var.availability_zones) == 0 ? null : [for n in range(0, var.cluster_size) : element(var.availability_zones, n)]
automatic_failover_enabled = var.cluster_mode_enabled ? true : var.automatic_failover_enabled
multi_az_enabled = var.multi_az_enabled
Expand Down
9 changes: 9 additions & 0 deletions variables.tf
Expand Up @@ -254,3 +254,12 @@ variable "auto_minor_version_upgrade" {
default = null
description = "Specifies whether minor version engine upgrades will be applied automatically to the underlying Cache Cluster instances during the maintenance window. Only supported if the engine version is 6 or higher."
}

variable "parameter_group_create_before_destroy" {
type = bool
description = <<-EOT
Set `true` to enable terraform `create_before_destroy` behavior on the created parameter group.
Note that changing this value will cause the parameter group to be replaced.
EOT
default = true
}
4 changes: 4 additions & 0 deletions versions.tf
Expand Up @@ -6,5 +6,9 @@ terraform {
source = "hashicorp/aws"
version = ">= 4.18"
}
random = {
source = "hashicorp/random"
version = ">= 3.0"
}
}
}