forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.tf
More file actions
174 lines (143 loc) · 5.29 KB
/
Copy pathredis.tf
File metadata and controls
174 lines (143 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# ─── Redis Auth Token ────────────────────────────────────────────────────────
resource "random_password" "redis_auth_token" {
length = 32
special = false # ElastiCache auth tokens must be printable ASCII, no spaces
}
resource "aws_secretsmanager_secret" "redis_auth_token" {
name = "nova-rewards/${var.environment}/redis-auth-token"
description = "Redis AUTH token for Nova Rewards ElastiCache cluster"
recovery_window_in_days = 7
}
resource "aws_secretsmanager_secret_version" "redis_auth_token" {
secret_id = aws_secretsmanager_secret.redis_auth_token.id
secret_string = jsonencode({
auth_token = random_password.redis_auth_token.result
})
}
# ─── Security Group ──────────────────────────────────────────────────────────
resource "aws_security_group" "redis" {
name = "nova-rewards-${var.environment}-redis"
description = "Allow inbound Redis traffic from the application security group only"
vpc_id = var.vpc_id
ingress {
description = "Redis from app instances"
from_port = 6379
to_port = 6379
protocol = "tcp"
security_groups = [var.app_security_group_id]
}
egress {
description = "Allow all outbound"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "nova-rewards-${var.environment}-redis"
Environment = var.environment
}
}
# ─── ElastiCache Subnet Group ────────────────────────────────────────────────
resource "aws_elasticache_subnet_group" "redis" {
name = "nova-rewards-${var.environment}-redis"
subnet_ids = var.private_subnet_ids
tags = {
Name = "nova-rewards-${var.environment}-redis"
Environment = var.environment
}
}
# ─── ElastiCache Redis Cluster ───────────────────────────────────────────────
resource "aws_elasticache_replication_group" "redis" {
replication_group_id = "nova-rewards-${var.environment}"
description = "Nova Rewards Redis cache — ${var.environment}"
node_type = "cache.t3.micro"
num_cache_clusters = 1
engine = "redis"
engine_version = "7.1"
port = 6379
# Encryption
at_rest_encryption_enabled = true
transit_encryption_enabled = true
transit_encryption_mode = "required"
auth_token = random_password.redis_auth_token.result
# Network — private subnets only, no public access
subnet_group_name = aws_elasticache_subnet_group.redis.name
security_group_ids = [aws_security_group.redis.id]
# Maintenance & backups
maintenance_window = "sun:05:00-sun:06:00"
snapshot_retention_limit = 1
snapshot_window = "04:00-05:00"
apply_immediately = false
tags = {
Name = "nova-rewards-${var.environment}-redis"
Environment = var.environment
}
}
# ─── CloudWatch Alarms ───────────────────────────────────────────────────────
locals {
cluster_id = aws_elasticache_replication_group.redis.id
}
resource "aws_cloudwatch_metric_alarm" "redis_cpu" {
alarm_name = "nova-rewards-${var.environment}-redis-cpu-high"
alarm_description = "Redis EngineCPUUtilization exceeded 80%"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "EngineCPUUtilization"
namespace = "AWS/ElastiCache"
period = 60
statistic = "Average"
threshold = 80
dimensions = {
ReplicationGroupId = local.cluster_id
}
alarm_actions = var.alarm_actions
ok_actions = var.alarm_actions
tags = {
Environment = var.environment
}
}
# CacheHitRate = CacheHits / (CacheHits + CacheMisses)
# CloudWatch metric math: if rate < 0.5 → alarm
resource "aws_cloudwatch_metric_alarm" "redis_hit_rate" {
alarm_name = "nova-rewards-${var.environment}-redis-low-hit-rate"
alarm_description = "Redis cache hit rate dropped below 50%"
comparison_operator = "LessThanThreshold"
evaluation_periods = 3
threshold = 0.5
metric_query {
id = "hit_rate"
expression = "hits / (hits + misses)"
label = "CacheHitRate"
return_data = true
}
metric_query {
id = "hits"
metric {
metric_name = "CacheHits"
namespace = "AWS/ElastiCache"
period = 60
stat = "Sum"
dimensions = {
ReplicationGroupId = local.cluster_id
}
}
}
metric_query {
id = "misses"
metric {
metric_name = "CacheMisses"
namespace = "AWS/ElastiCache"
period = 60
stat = "Sum"
dimensions = {
ReplicationGroupId = local.cluster_id
}
}
}
alarm_actions = var.alarm_actions
ok_actions = var.alarm_actions
tags = {
Environment = var.environment
}
}