Skip to content

Commit dadbc87

Browse files
Merge pull request #3809 from nhsuk/create_valkey_config_for_sidekiq
Complete implementation of valkey with a connected sidekiq service
2 parents 605b88b + c229b8f commit dadbc87

File tree

12 files changed

+410
-6
lines changed

12 files changed

+410
-6
lines changed

bin/docker-start

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [ "$SERVER_TYPE" == "web" ]; then
88
elif [ "$SERVER_TYPE" == "good-job" ]; then
99
echo "Starting good-job server..."
1010
exec "$BIN_DIR"/good_job start
11-
elif [ "$SERVER_TYPE" == "none" ]; then
11+
elif [ "$SERVER_TYPE" == "none" ] || [ "$SERVER_TYPE" == "sidekiq" ]; then #TODO: Implement sidekiq in application
1212
echo "No server started"
1313
exec tail -f /dev/null # Keep container running
1414
else

terraform/account/resources/iam_policy_DeployMavisResources.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@
9292
"iam:DetachRolePolicy",
9393
"kms:CreateGrant",
9494
"kms:Decrypt",
95+
"logs:PutResourcePolicy",
96+
"logs:DescribeResourcePolicies",
97+
"logs:DescribeLogGroups",
98+
"logs:CreateLogDelivery",
99+
"logs:UpdateLogDelivery",
100+
"logs:DeleteLogDelivery",
101+
"logs:GetLogDelivery",
102+
"logs:ListLogDeliveries",
95103
"logs:CreateLogGroup",
96104
"logs:DeleteLogGroup",
97105
"logs:PutRetentionPolicy",
@@ -133,7 +141,21 @@
133141
"secretsmanager:CancelRotateSecret",
134142
"ssm:DeleteParameter",
135143
"ssm:DeleteParameters",
136-
"ssm:PutParameter"
144+
"ssm:PutParameter",
145+
"elasticache:CreateCacheParameterGroup",
146+
"elasticache:AuthorizeCacheSecurityGroupIngress",
147+
"elasticache:CreateReplicationGroup",
148+
"elasticache:CreateCacheSubnetGroup",
149+
"elasticache:DecreaseReplicaCount",
150+
"elasticache:DeleteCacheCluster",
151+
"elasticache:DeleteCacheParameterGroup",
152+
"elasticache:DeleteCacheSubnetGroup",
153+
"elasticache:DeleteReplicationGroup",
154+
"elasticache:ModifyReplicationGroup",
155+
"elasticache:ModifyCacheCluster",
156+
"elasticache:ModifyCacheParameterGroup",
157+
"elasticache:ModifyCacheSubnetGroup",
158+
"elasticache:IncreaseReplicaCount"
137159
],
138160
"Resource": ["*"]
139161
}

terraform/app/ecs.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,33 @@ module "good_job_service" {
8383
environment = var.environment
8484
server_type = "good-job"
8585
}
86+
87+
module "sidekiq_service" {
88+
source = "./modules/ecs_service"
89+
task_config = {
90+
environment = local.task_envs
91+
secrets = local.task_secrets
92+
cpu = 1024
93+
memory = 2048
94+
docker_image = "${var.account_id}.dkr.ecr.eu-west-2.amazonaws.com/${var.docker_image}@${var.image_digest}"
95+
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
96+
task_role_arn = aws_iam_role.ecs_task_role.arn
97+
log_group_name = aws_cloudwatch_log_group.ecs_log_group.name
98+
region = var.region
99+
health_check_command = ["CMD-SHELL", "echo true || exit 1"]
100+
}
101+
network_params = {
102+
subnets = [aws_subnet.private_subnet_a.id, aws_subnet.private_subnet_b.id]
103+
vpc_id = aws_vpc.application_vpc.id
104+
}
105+
minimum_replica_count = var.sidekiq_replicas
106+
maximum_replica_count = var.sidekiq_replicas
107+
cluster_id = aws_ecs_cluster.cluster.id
108+
cluster_name = aws_ecs_cluster.cluster.name
109+
environment = var.environment
110+
server_type = "sidekiq"
111+
112+
depends_on = [
113+
aws_elasticache_replication_group.valkey
114+
]
115+
}

terraform/app/env/preview.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ http_hosts = {
2222
appspec_bucket = "nhse-mavis-appspec-bucket-preview"
2323
minimum_web_replicas = 2
2424
maximum_web_replicas = 4
25+
26+
valkey_node_type = "cache.t4g.micro"
27+
valkey_log_retention_days = 3
28+
valkey_failover_enabled = false

terraform/app/env/sandbox-alpha.tfvars

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ appspec_bucket = "nhse-mavis-appspec-bucket-sandbox-alpha"
2020
minimum_web_replicas = 1
2121
maximum_web_replicas = 2
2222
good_job_replicas = 1
23+
24+
valkey_node_type = "cache.t4g.micro"
25+
valkey_log_retention_days = 3
26+
valkey_failover_enabled = false
27+
sidekiq_replicas = 1

terraform/app/env/sandbox-beta.tfvars

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ appspec_bucket = "nhse-mavis-appspec-bucket-sandbox-beta"
2020
minimum_web_replicas = 1
2121
maximum_web_replicas = 2
2222
good_job_replicas = 1
23+
24+
# Valkey serverless configuration - minimal settings for sandbox
25+
valkey_node_type = "cache.t4g.micro"
26+
valkey_log_retention_days = 3
27+
valkey_failover_enabled = false
28+
sidekiq_replicas = 1

terraform/app/env/test.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ http_hosts = {
2020
appspec_bucket = "nhse-mavis-appspec-bucket-test"
2121
minimum_web_replicas = 2
2222
maximum_web_replicas = 4
23+
24+
valkey_node_type = "cache.t4g.micro"
25+
valkey_log_retention_days = 3
26+
valkey_failover_enabled = false

terraform/app/env/training.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ http_hosts = {
2424
appspec_bucket = "nhse-mavis-appspec-bucket-training"
2525
minimum_web_replicas = 2
2626
maximum_web_replicas = 4
27+
28+
valkey_node_type = "cache.t4g.micro"
29+
valkey_log_retention_days = 3
30+
valkey_failover_enabled = false

terraform/app/outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ output "ecs_variables" {
3030
service_name = module.good_job_service.service.name
3131
task_definition = module.good_job_service.task_definition
3232
}
33+
sidekiq = {
34+
service_name = module.sidekiq_service.service.name
35+
task_definition = module.sidekiq_service.task_definition
36+
}
3337
}
34-
description = "Essential attributes of the ECS service"
38+
description = "Essential attributes of the ECS services"
3539
}
3640

3741
output "db_secret_arn" {

terraform/app/valkey.tf

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
resource "aws_security_group" "valkey" {
2+
name = "mavis-cache-${var.environment}"
3+
description = "Security group for Valkey ElastiCache (self-designed cluster)"
4+
vpc_id = aws_vpc.application_vpc.id
5+
6+
tags = {
7+
Name = "mavis-cache-${var.environment}"
8+
}
9+
10+
lifecycle {
11+
ignore_changes = [description]
12+
}
13+
}
14+
15+
resource "aws_security_group_rule" "valkey_ecs_services_ingress" {
16+
count = length(local.ecs_sg_ids)
17+
type = "ingress"
18+
from_port = var.valkey_port
19+
to_port = var.valkey_port
20+
protocol = "tcp"
21+
security_group_id = aws_security_group.valkey.id
22+
source_security_group_id = local.ecs_sg_ids[count.index]
23+
24+
lifecycle {
25+
create_before_destroy = true
26+
}
27+
}
28+
29+
resource "aws_elasticache_subnet_group" "valkey" {
30+
name = "mavis-cache-subnet-group-${var.environment}"
31+
subnet_ids = [aws_subnet.private_subnet_a.id, aws_subnet.private_subnet_b.id]
32+
33+
tags = {
34+
Name = "mavis-cache-subnet-group-${var.environment}"
35+
}
36+
}
37+
38+
resource "aws_elasticache_replication_group" "valkey" {
39+
replication_group_id = "mavis-cache-${var.environment}"
40+
description = "Valkey cluster for Sidekiq"
41+
42+
engine = "valkey"
43+
engine_version = var.valkey_engine_version
44+
node_type = var.valkey_node_type
45+
port = var.valkey_port
46+
parameter_group_name = aws_elasticache_parameter_group.valkey.name
47+
48+
automatic_failover_enabled = var.valkey_failover_enabled
49+
num_cache_clusters = length(local.valkey_cache_availability_zones)
50+
subnet_group_name = aws_elasticache_subnet_group.valkey.name
51+
security_group_ids = [aws_security_group.valkey.id]
52+
preferred_cache_cluster_azs = local.valkey_cache_availability_zones
53+
snapshot_retention_limit = var.valkey_snapshot_retention_limit
54+
snapshot_window = var.valkey_snapshot_window
55+
maintenance_window = var.valkey_maintenance_window
56+
57+
at_rest_encryption_enabled = true
58+
transit_encryption_enabled = true
59+
60+
log_delivery_configuration {
61+
destination = aws_cloudwatch_log_group.valkey_slow_log.name
62+
destination_type = "cloudwatch-logs"
63+
log_format = "json"
64+
log_type = "slow-log"
65+
}
66+
67+
log_delivery_configuration {
68+
destination = aws_cloudwatch_log_group.valkey_engine_log.name
69+
destination_type = "cloudwatch-logs"
70+
log_format = "json"
71+
log_type = "engine-log"
72+
}
73+
74+
tags = {
75+
Name = "mavis-cache-${var.environment}"
76+
Purpose = "sidekiq-job-processing"
77+
}
78+
apply_immediately = true
79+
}
80+
81+
resource "aws_elasticache_parameter_group" "valkey" {
82+
family = "valkey8"
83+
name = "mavis-cache-params-${var.environment}"
84+
85+
# Optimize for Sidekiq workload
86+
parameter {
87+
name = "maxmemory-policy"
88+
value = "noeviction"
89+
}
90+
91+
tags = {
92+
Name = "mavis-cache-params-${var.environment}"
93+
}
94+
}
95+
96+
resource "aws_cloudwatch_log_group" "valkey_slow_log" {
97+
name = "/aws/elasticache/valkey/${var.environment}/slow-log"
98+
retention_in_days = var.valkey_log_retention_days
99+
100+
tags = {
101+
Name = "mavis-cache-slow-log-${var.environment}"
102+
}
103+
}
104+
105+
resource "aws_cloudwatch_log_group" "valkey_engine_log" {
106+
name = "/aws/elasticache/valkey/${var.environment}/engine-log"
107+
retention_in_days = var.valkey_log_retention_days
108+
109+
tags = {
110+
Name = "mavis-cache-engine-log-${var.environment}"
111+
}
112+
}

0 commit comments

Comments
 (0)