-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring.tf
More file actions
230 lines (222 loc) · 8.09 KB
/
Copy pathmonitoring.tf
File metadata and controls
230 lines (222 loc) · 8.09 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
locals {
alarm_actions = var.alarm_sns_topic_arn != null ? [var.alarm_sns_topic_arn] : []
}
# ── Layer 5 – Block-rate alarm ────────────────────────────────────────────────
# The guardrail publishes two counters per action evaluation:
# BedrockGuardrail/<prefix> BlockedActions (action was blocked)
# BedrockGuardrail/<prefix> ApprovedActions (action was approved / executed)
#
# Block rate = BlockedActions / (BlockedActions + ApprovedActions)
# Alarm fires when rate drops BELOW 0.70 in any 5-minute window.
# This triggers automatic rollback via the rollback_executor Lambda.
resource "aws_cloudwatch_metric_alarm" "block_rate_low" {
alarm_name = "${local.name_prefix}-guardrail-block-rate-low"
alarm_description = "Guardrail block rate < 70% in 5 min — possible bypass. Triggers S3 rollback."
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
threshold = var.block_rate_alarm_threshold * 100 # alarm math expression yields 0–100
# Math expression: block_rate_pct = blocked / (blocked + approved) * 100
metric_query {
id = "block_rate_pct"
expression = "(blocked / (blocked + approved + 0.001)) * 100"
label = "Block Rate %"
return_data = true
}
metric_query {
id = "blocked"
metric {
namespace = local.metric_namespace
metric_name = "BlockedActions"
period = 300
stat = "Sum"
}
}
metric_query {
id = "approved"
metric {
namespace = local.metric_namespace
metric_name = "ApprovedActions"
period = 300
stat = "Sum"
}
}
treat_missing_data = "notBreaching"
# Alarm action: invoke the rollback executor Lambda
alarm_actions = concat(
local.alarm_actions,
[aws_lambda_function.rollback_executor.arn]
)
ok_actions = local.alarm_actions
tags = local.common_tags
}
# ── HITL expiry rate alarm ─────────────────────────────────────────────────────
resource "aws_cloudwatch_metric_alarm" "hitl_expiry_spike" {
alarm_name = "${local.name_prefix}-hitl-expiry-spike"
alarm_description = "Multiple HITL windows expired without approval — possible approver unavailability"
namespace = local.metric_namespace
metric_name = "HITLAutoRejected"
statistic = "Sum"
period = 600
evaluation_periods = 1
threshold = 3
comparison_operator = "GreaterThanOrEqualToThreshold"
treat_missing_data = "notBreaching"
alarm_actions = local.alarm_actions
tags = local.common_tags
}
# ── SFN execution failure alarm ────────────────────────────────────────────────
resource "aws_cloudwatch_metric_alarm" "sfn_failures" {
alarm_name = "${local.name_prefix}-sfn-execution-failures"
alarm_description = "Guardrail orchestration failures > 3 in 5 minutes"
namespace = "AWS/States"
metric_name = "ExecutionsFailed"
dimensions = { StateMachineArn = aws_sfn_state_machine.guardrail.arn }
statistic = "Sum"
period = 300
evaluation_periods = 1
threshold = 3
comparison_operator = "GreaterThanThreshold"
treat_missing_data = "notBreaching"
alarm_actions = local.alarm_actions
ok_actions = local.alarm_actions
tags = local.common_tags
}
# ── CloudWatch Dashboard ──────────────────────────────────────────────────────
resource "aws_cloudwatch_dashboard" "guardrail" {
count = 0
dashboard_name = "${local.name_prefix}-guardrail"
dashboard_body = jsonencode({
widgets = [
# Row 1: Action routing breakdown
{
type = "metric"
x = 0
y = 0
width = 8
height = 6
properties = {
title = "Action Routing by Blast Radius"
view = "timeSeries"
period = 60
stat = "Sum"
metrics = [
[local.metric_namespace, "AutoApproved", { label = "Auto-Approved (B<0.3)" }],
[local.metric_namespace, "RoutedL1L2", { label = "L1+L2 Only (0.3≤B<0.9)" }],
[local.metric_namespace, "RoutedFullPipeline", { label = "Full Pipeline (B≥0.9)" }],
]
}
},
# Row 1: Block rate
{
type = "metric"
x = 8
y = 0
width = 8
height = 6
properties = {
title = "Block Rate % (alarm threshold: 70%)"
view = "timeSeries"
period = 300
metrics = [
[{ id = "e1", expression = "(blocked/(blocked+approved+0.001))*100", label = "Block Rate %" }],
[local.metric_namespace, "BlockedActions", { id = "blocked", visible = false }],
[local.metric_namespace, "ApprovedActions", { id = "approved", visible = false }],
]
annotations = { horizontal = [{ value = 70, label = "Rollback threshold", color = "#d62728" }] }
}
},
# Row 1: HITL outcomes
{
type = "metric"
x = 16
y = 0
width = 8
height = 6
properties = {
title = "Layer 4 HITL Outcomes"
view = "timeSeries"
period = 60
stat = "Sum"
metrics = [
[local.metric_namespace, "HITLApproved", { label = "Approved" }],
[local.metric_namespace, "HITLRejected", { label = "Manually Rejected" }],
[local.metric_namespace, "HITLAutoRejected", { label = "Auto-Rejected (TTL)" }],
]
}
},
# Row 2: Confidence delta distribution
{
type = "metric"
x = 0
y = 6
width = 12
height = 6
properties = {
title = "Layer 3 Confidence Delta Distribution"
view = "timeSeries"
period = 60
metrics = [
[local.metric_namespace, "ConfidenceDelta", { stat = "Average", label = "Avg Delta" }],
[local.metric_namespace, "ConfidenceDelta", { stat = "p95", label = "p95 Delta" }],
[local.metric_namespace, "DeltaEscalations", { stat = "Sum", label = "Escalations (≥0.55)" }],
]
annotations = { horizontal = [{ value = 0.55, label = "Escalation threshold" }] }
}
},
# Row 2: Step Functions execution time
{
type = "metric"
x = 12
y = 6
width = 12
height = 6
properties = {
title = "SFN Execution Duration (ms)"
view = "timeSeries"
period = 60
metrics = [
["AWS/States", "ExecutionTime", "StateMachineArn", aws_sfn_state_machine.guardrail.arn, { stat = "p50", label = "p50" }],
["AWS/States", "ExecutionTime", "StateMachineArn", aws_sfn_state_machine.guardrail.arn, { stat = "p95", label = "p95" }],
]
}
},
# Row 3: Rollback events
{
type = "metric"
x = 0
y = 12
width = 12
height = 6
properties = {
title = "Layer 5 Rollback Events"
view = "timeSeries"
period = 300
stat = "Sum"
metrics = [
[local.metric_namespace, "RollbackTriggered", { label = "Triggered" }],
[local.metric_namespace, "RollbackSucceeded", { label = "Succeeded" }],
[local.metric_namespace, "RollbackFailed", { label = "Failed", color = "#d62728" }],
]
}
},
# Row 3: OPA policy violations
{
type = "metric"
x = 12
y = 12
width = 12
height = 6
properties = {
title = "Layer 2 OPA Policy Violations"
view = "timeSeries"
period = 60
stat = "Sum"
metrics = [
[local.metric_namespace, "OPAAllowed", { label = "Allowed" }],
[local.metric_namespace, "OPADenied", { label = "Denied" }],
]
}
},
]
})
}