1+ resource "aws_cloudwatch_metric_alarm" "high_latency" {
2+ alarm_name = " ALB-High-Latency"
3+ comparison_operator = " GreaterThanThreshold"
4+ evaluation_periods = 1
5+ metric_name = " TargetResponseTime"
6+ namespace = " AWS/ApplicationELB"
7+ period = 60 # 1 min
8+ statistic = " Average"
9+ threshold = 1 # 1 second
10+
11+ dimensions = {
12+ LoadBalancer = aws_lb.wfprev_main.arn_suffix
13+ }
14+
15+ alarm_description = " ALB target response time is high"
16+ alarm_actions = [aws_sns_topic . alb_alerts . arn ]
17+ }
18+
19+ resource "aws_cloudwatch_metric_alarm" "unhealthy_targets" {
20+ alarm_name = " ALB-Unhealthy-Targets"
21+ comparison_operator = " GreaterThanThreshold"
22+ evaluation_periods = 1
23+ metric_name = " UnHealthyHostCount"
24+ namespace = " AWS/ApplicationELB"
25+ period = 60
26+ statistic = " Average"
27+ threshold = 0
28+
29+ dimensions = {
30+ TargetGroup = aws_alb_target_group.wfprev_api.arn_suffix
31+ LoadBalancer = aws_lb.wfprev_main.arn_suffix
32+ }
33+
34+ alarm_description = " ALB has unhealthy targets"
35+ alarm_actions = [aws_sns_topic . alb_alerts . arn ]
36+ }
37+
38+ resource "aws_sns_topic" "alb_alerts" {
39+ name = " wfprev-alb-alerts"
40+ }
41+
42+ # List of emails
43+ locals {
44+ alert_emails = split (" ," , var. AWS_ALERT_EMAIL_LIST )
45+ }
46+
47+ # Create subscriptions for each email
48+ resource "aws_sns_topic_subscription" "alb_alerts_emails" {
49+ for_each = toset (local. alert_emails )
50+
51+ topic_arn = aws_sns_topic. alb_alerts . arn
52+ protocol = " email"
53+ endpoint = trim (each. key , " " )
54+ }
0 commit comments