Skip to content

Commit

Permalink
Merge pull request #1726 from alphagov/samsimpson1/spike-elasticache
Browse files Browse the repository at this point in the history
Elasticache spike: Rework elasticache module to create a shared instance
  • Loading branch information
samsimpson1 authored Mar 4, 2025
2 parents dd0e294 + 84c6c0a commit 54fa402
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 34 deletions.
55 changes: 33 additions & 22 deletions terraform/deployments/elasticache/elasticache.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,50 @@ locals {
}

resource "aws_security_group" "cache" {
for_each = var.instances
name = "elasticache-${each.key}"
name = "elasticache-shared"
vpc_id = data.tfe_outputs.vpc.nonsensitive_values.id
description = "EKS to ElastiCache instance ${each.key} (govuk-infrastructure/terraform/deployments/elasticache)"
description = "EKS to shared ElastiCache instance (govuk-infrastructure/terraform/deployments/elasticache)"
}

resource "aws_vpc_security_group_ingress_rule" "cache" {
for_each = var.instances
security_group_id = aws_security_group.cache[each.key].id
security_group_id = aws_security_group.cache.id

from_port = 6379
to_port = 6379
ip_protocol = "tcp"
referenced_security_group_id = data.tfe_outputs.cluster_infrastructure.nonsensitive_values.node_security_group_id
}

resource "aws_elasticache_serverless_cache" "cache" {
for_each = var.instances
name = each.key
engine = "valkey"
major_engine_version = try(each.value.major_engine_version, local.default_engine_version)
security_group_ids = [aws_security_group.cache[each.key].id]
subnet_ids = data.tfe_outputs.cluster_infrastructure.nonsensitive_values.private_subnets

cache_usage_limits {
data_storage {
maximum = try(each.value.max_storage_gb, local.default_max_storage_gb)
unit = "GB"
}
ecpu_per_second {
maximum = try(each.value.max_ecpus_per_second, local.default_max_ecpus_per_second)
}
resource "aws_elasticache_subnet_group" "cache" {
name = "elasticache-shared"
subnet_ids = data.tfe_outputs.cluster_infrastructure.nonsensitive_values.private_subnets
}

resource "aws_elasticache_parameter_group" "cache" {
name = "elasticache-shared"
family = "valkey8"

parameter {
name = "databases"
value = 10000
}

parameter {
name = "maxmemory-policy"
value = "noeviction"
}
}

resource "aws_elasticache_replication_group" "cache" {
replication_group_id = "govuk-shared"
description = "Shared Valkey"
num_cache_clusters = 1
node_type = var.node_type
engine = "valkey"
engine_version = var.engine_version
parameter_group_name = aws_elasticache_parameter_group.cache.name
subnet_group_name = aws_elasticache_subnet_group.cache.name
security_group_ids = [aws_security_group.cache.id]
}

resource "aws_secretsmanager_secret" "urls" {
Expand All @@ -46,5 +57,5 @@ resource "aws_secretsmanager_secret" "urls" {

resource "aws_secretsmanager_secret_version" "urls" {
secret_id = "govuk/elasticache/urls"
secret_string = jsonencode({ for name, cache in aws_elasticache_serverless_cache.cache : name => "rediss://${cache.endpoint[0].address}:${cache.endpoint[0].port}" })
secret_string = jsonencode({ for app, dbId in var.databases : app => "redis://${aws_elasticache_replication_group.cache.primary_endpoint_address}:6379/${dbId}" })
}
18 changes: 15 additions & 3 deletions terraform/deployments/elasticache/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ variable "govuk_environment" {
description = "GOV.UK environment name"
}

variable "instances" {
type = map(any)
description = "Map of instance name -> settings"
variable "databases" {
type = map(number)
description = "Map of app names to database IDs"
}

variable "engine_version" {
type = string
default = "8.0"
description = "Valkey version"
}

variable "node_type" {
type = string
default = "cache.m7g.xlarge"
description = "ElastiCache node type"
}
43 changes: 34 additions & 9 deletions terraform/deployments/tfc-configuration/variables-integration.tf
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,40 @@ module "variable-set-elasticache-integration" {
name = "elasticache-integration"

tfvars = {
instances = {
/*
"example" = {
max_storage_gb = 30
max_ecpus_per_second = 7000
major_engine_version = "7"
}
*/
"publishing-api" = {}

# a map of app names to database IDs
databases = {
"account-api" = 0
"asset-manager" = 1
"collections-publisher" = 2
"contacts-admin" = 3
"content-data-admin" = 4
"content-data-api" = 5
"content-publisher" = 6
"content-tagger" = 7
"draft-email-alert-frontend" = 8
"email-alert-api" = 9
"email-alert-frontend" = 10
"email-alert-service" = 11
"link-checker-api" = 12
"local-links-manager" = 13
"locations-api" = 14
"manuals-publisher" = 15
"places-manager" = 16
"publisher-on-pg" = 17
"publisher" = 18
"publishing-api" = 19
"search-admin" = 20
"search-api" = 21
"search-api-v2" = 22
"short-url-manager" = 23
"signon" = 24
"specialist-publisher" = 25
"support-api" = 26
"support" = 27
"transition" = 28
"travel-advice-publisher" = 29
"whitehall-admin" = 30
}
}
}

0 comments on commit 54fa402

Please sign in to comment.