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