Skip to content

Commit 7f39cf0

Browse files
Merge pull request #4647 from nhsuk/rails_cache_for_redis
Introduce a rails cache for web server
2 parents 6d5e946 + 91df2cf commit 7f39cf0

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

terraform/app/ecs.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ resource "aws_ecs_cluster" "cluster" {
2222
module "web_service" {
2323
source = "./modules/ecs_service"
2424
task_config = {
25-
environment = local.task_envs
25+
environment = concat(
26+
local.task_envs,
27+
[
28+
{
29+
name = "REDIS_CACHE_URL"
30+
value = aws_elasticache_serverless_cache.rails_cache.endpoint[0].address
31+
}
32+
]
33+
)
2634
secrets = local.task_secrets
2735
cpu = 1024
2836
memory = 3072

terraform/app/valkey.tf

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
################ SIDEKIQ CACHE FOR JOB PROCESSING #####################
2+
13
resource "aws_security_group" "valkey" {
24
name = "mavis-cache-${var.environment}"
35
description = "Security group for Valkey ElastiCache (self-designed cluster)"
@@ -109,4 +111,66 @@ resource "aws_cloudwatch_log_group" "valkey_engine_log" {
109111
tags = {
110112
Name = "mavis-cache-engine-log-${var.environment}"
111113
}
112-
}
114+
}
115+
116+
117+
################ REDIS CACHE FOR CACHING DB QUERIES #####################
118+
119+
resource "aws_security_group" "rails_valkey" {
120+
name = "mavis-cache-rails-${var.environment}"
121+
description = "Security group for Valkey ElastiCache for the rails service"
122+
vpc_id = aws_vpc.application_vpc.id
123+
124+
tags = {
125+
Name = "mavis-cache-rails-${var.environment}"
126+
}
127+
128+
lifecycle {
129+
ignore_changes = [description]
130+
}
131+
}
132+
133+
resource "aws_security_group_rule" "rails_valkey_ingress" {
134+
type = "ingress"
135+
from_port = aws_elasticache_serverless_cache.rails_cache.endpoint[0].port
136+
to_port = aws_elasticache_serverless_cache.rails_cache.endpoint[0].port
137+
protocol = "tcp"
138+
security_group_id = aws_security_group.rails_valkey.id
139+
source_security_group_id = module.web_service.security_group_id
140+
141+
lifecycle {
142+
create_before_destroy = true
143+
}
144+
}
145+
146+
resource "aws_elasticache_parameter_group" "rails" {
147+
family = "valkey8"
148+
name = "mavis-cache-rails-params-${var.environment}"
149+
150+
parameter {
151+
name = "maxmemory-policy"
152+
value = "allkeys-lfu"
153+
}
154+
155+
tags = {
156+
Name = "mavis-cache-rails-params-${var.environment}"
157+
}
158+
}
159+
160+
resource "aws_elasticache_serverless_cache" "rails_cache" {
161+
engine = "valkey"
162+
name = "mavis-cache-rails-${var.environment}"
163+
description = "Rails cache for web servers"
164+
cache_usage_limits {
165+
data_storage {
166+
maximum = 1
167+
unit = "GB"
168+
}
169+
ecpu_per_second {
170+
maximum = 1000
171+
}
172+
}
173+
major_engine_version = "8"
174+
security_group_ids = [aws_security_group.rails_valkey.id]
175+
subnet_ids = [aws_subnet.private_subnet_a.id, aws_subnet.private_subnet_b.id]
176+
}

0 commit comments

Comments
 (0)