@@ -261,6 +261,141 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
261261 )
262262}
263263
264+ # Alarm for Evictions
265+ resource "aws_cloudwatch_metric_alarm" "cache_evictions" {
266+ count = var. cloudwatch_metric_alarms_enabled ? 1 : 0
267+ alarm_name = format (" %s-%s-%s" , var. environment , var. name , " evictions" )
268+ alarm_description = " Redis evictions due to memory pressure"
269+ comparison_operator = " GreaterThanThreshold"
270+ evaluation_periods = " 1"
271+ metric_name = " Evictions"
272+ namespace = " AWS/ElastiCache"
273+ period = " 300"
274+ statistic = " Sum"
275+ threshold = var. alarm_eviction_threshold
276+
277+ dimensions = {
278+ CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0 ].id
279+ }
280+
281+ alarm_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
282+ ok_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
283+ depends_on = [aws_sns_topic . slack_topic ]
284+
285+ tags = merge (
286+ { " Name" = format (" %s-%s-%s" , var. environment , var. name , " eviction_metric" ) },
287+ local. tags ,
288+ )
289+ }
290+
291+ # Alarm for Connections
292+ resource "aws_cloudwatch_metric_alarm" "cache_connections" {
293+ count = var. cloudwatch_metric_alarms_enabled ? 1 : 0
294+ alarm_name = format (" %s-%s-%s" , var. environment , var. name , " connections" )
295+ alarm_description = " Redis cluster number of client connections"
296+ comparison_operator = " GreaterThanThreshold"
297+ evaluation_periods = " 1"
298+ metric_name = " CurrConnections"
299+ namespace = " AWS/ElastiCache"
300+ period = " 300"
301+ statistic = " Average"
302+ threshold = var. alarm_connections_threshold
303+
304+ dimensions = {
305+ CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0 ].id
306+ }
307+
308+ alarm_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
309+ ok_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
310+ depends_on = [aws_sns_topic . slack_topic ]
311+
312+ tags = merge (
313+ { " Name" = format (" %s-%s-%s" , var. environment , var. name , " connections_metric" ) },
314+ local. tags ,
315+ )
316+ }
317+
318+ # Alarm for Replication Lag (if using replication)
319+ resource "aws_cloudwatch_metric_alarm" "cache_replication_lag" {
320+ count = var. cloudwatch_metric_alarms_enabled && var. num_cache_nodes > 1 ? 1 : 0
321+ alarm_name = format (" %s-%s-%s" , var. environment , var. name , " replication-lag" )
322+ alarm_description = " Redis replication lag"
323+ comparison_operator = " GreaterThanThreshold"
324+ evaluation_periods = " 1"
325+ metric_name = " ReplicationLag"
326+ namespace = " AWS/ElastiCache"
327+ period = " 300"
328+ statistic = " Maximum"
329+ threshold = var. alarm_replication_lag_threshold
330+
331+ dimensions = {
332+ ReplicationGroupId = aws_elasticache_replication_group.redis[count.index].id
333+ }
334+
335+ alarm_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
336+ ok_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
337+ depends_on = [aws_sns_topic . slack_topic ]
338+
339+ tags = merge (
340+ { " Name" = format (" %s-%s-%s" , var. environment , var. name , " replication_lag_metric" ) },
341+ local. tags ,
342+ )
343+ }
344+
345+ # Alarm for Cache Hits
346+ resource "aws_cloudwatch_metric_alarm" "cache_hits" {
347+ count = var. cloudwatch_metric_alarms_enabled ? 1 : 0
348+ alarm_name = format (" %s-%s-%s" , var. environment , var. name , " cache-hits" )
349+ alarm_description = " Redis cache hits"
350+ comparison_operator = " LessThanThreshold"
351+ evaluation_periods = " 1"
352+ metric_name = " CacheHits"
353+ namespace = " AWS/ElastiCache"
354+ period = " 300"
355+ statistic = " Sum"
356+ threshold = var. alarm_cache_hits_threshold
357+
358+ dimensions = {
359+ CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0 ].id
360+ }
361+
362+ alarm_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
363+ ok_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
364+ depends_on = [aws_sns_topic . slack_topic ]
365+
366+ tags = merge (
367+ { " Name" = format (" %s-%s-%s" , var. environment , var. name , " cache_hits_metric" ) },
368+ local. tags ,
369+ )
370+ }
371+
372+ # Alarm for Cache Misses
373+ resource "aws_cloudwatch_metric_alarm" "cache_misses" {
374+ count = var. cloudwatch_metric_alarms_enabled ? 1 : 0
375+ alarm_name = format (" %s-%s-%s" , var. environment , var. name , " cache-misses" )
376+ alarm_description = " Redis cache misses"
377+ comparison_operator = " GreaterThanThreshold"
378+ evaluation_periods = " 1"
379+ metric_name = " CacheMisses"
380+ namespace = " AWS/ElastiCache"
381+ period = " 300"
382+ statistic = " Sum"
383+ threshold = var. alarm_cache_misses_threshold
384+
385+ dimensions = {
386+ CacheClusterId = var.num_cache_nodes > 1 ? aws_elasticache_replication_group.redis[count.index].id : aws_elasticache_cluster.redis[0 ].id
387+ }
388+
389+ alarm_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
390+ ok_actions = [aws_sns_topic . slack_topic [0 ]. arn ]
391+ depends_on = [aws_sns_topic . slack_topic ]
392+
393+ tags = merge (
394+ { " Name" = format (" %s-%s-%s" , var. environment , var. name , " cache_misses_metric" ) },
395+ local. tags ,
396+ )
397+ }
398+
264399resource "aws_kms_key" "this" {
265400 count = var. slack_notification_enabled ? 1 : 0
266401 description = " KMS key for notify-slack test"
0 commit comments