The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as CloudWatch from "@alchemy.run/aws/CloudWatch"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 10 | |
| 11 | /** |
| 12 | * Custom metric namespace used by the fixture's alarm, anomaly detector and |
| 13 | * metric routes. Shared with Bindings.test.ts. |
| 14 | */ |
| 15 | export const METRIC_NAMESPACE = "Alchemy/CloudWatchBindings"; |
| 16 | export const METRIC_NAME = "BindingTestMetric"; |
| 17 | |
| 18 | export class CloudWatchTestFunction extends Lambda.Function<Lambda.Function>()( |
| 19 | "CloudWatchTestFunction", |
| 20 | ) {} |
| 21 | |
| 22 | export default CloudWatchTestFunction.make( |
| 23 | { |
| 24 | main, |
| 25 | functionUrl: true, |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | // A metric alarm on a custom metric. Threshold is unreachably high and |
| 29 | // TreatMissingData=notBreaching so the alarm sits in OK/INSUFFICIENT_DATA |
| 30 | // and never pages anything. |
| 31 | const alarm = yield* CloudWatch.Alarm("BindingAlarm", { |
| 32 | MetricName: METRIC_NAME, |
| 33 | Namespace: METRIC_NAMESPACE, |
| 34 | Statistic: "Sum", |
| 35 | Period: 60, |
| 36 | EvaluationPeriods: 1, |
| 37 | Threshold: 1_000_000, |
| 38 | ComparisonOperator: "GreaterThanOrEqualToThreshold", |
| 39 | TreatMissingData: "notBreaching", |
| 40 | }); |
| 41 | |
| 42 | const dashboard = yield* CloudWatch.Dashboard("BindingDashboard", { |
| 43 | DashboardBody: { |
| 44 | widgets: [ |
| 45 | { |
| 46 | type: "text", |
| 47 | x: 0, |
| 48 | y: 0, |
| 49 | width: 6, |
| 50 | height: 3, |
| 51 | properties: { |
| 52 | markdown: "# Alchemy CloudWatch bindings fixture", |
| 53 | }, |
| 54 | }, |
| 55 | ], |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | const insightRule = yield* CloudWatch.InsightRule("BindingInsightRule", { |
| 60 | RuleState: "ENABLED", |
| 61 | RuleDefinition: { |
| 62 | Schema: { |
| 63 | Name: "CloudWatchLogRule", |
| 64 | Version: 1, |
| 65 | }, |
| 66 | LogGroupNames: ["alchemy-test-cloudwatch-bindings-log-group"], |
| 67 | LogFormat: "JSON", |
| 68 | Contribution: { |
| 69 | Keys: ["$.ip"], |
| 70 | Filters: [], |
| 71 | }, |
| 72 | AggregateOn: "Count", |
| 73 | }, |
| 74 | }); |
| 75 | |
| 76 | const detector = yield* CloudWatch.AnomalyDetector("BindingAnomalyDetector", { |
| 77 | Namespace: METRIC_NAMESPACE, |
| 78 | MetricName: METRIC_NAME, |
| 79 | Stat: "Sum", |
| 80 | }); |
| 81 | yield* detector.detectorId; |
| 82 | |
| 83 | // Bindings under test (one per CloudWatch capability). |
| 84 | const describeAlarms = yield* CloudWatch.DescribeAlarms(alarm); |
| 85 | const describeAlarmContributors = yield* CloudWatch.DescribeAlarmContributors(alarm); |
| 86 | const describeAlarmHistory = yield* CloudWatch.DescribeAlarmHistory(); |
| 87 | const describeAlarmsForMetric = yield* CloudWatch.DescribeAlarmsForMetric(); |
| 88 | const describeAnomalyDetectors = yield* CloudWatch.DescribeAnomalyDetectors(); |
| 89 | const describeInsightRules = yield* CloudWatch.DescribeInsightRules(); |
| 90 | const disableAlarmActions = yield* CloudWatch.DisableAlarmActions(alarm); |
| 91 | const enableAlarmActions = yield* CloudWatch.EnableAlarmActions(alarm); |
| 92 | const disableInsightRules = yield* CloudWatch.DisableInsightRules(insightRule); |
| 93 | const enableInsightRules = yield* CloudWatch.EnableInsightRules(insightRule); |
| 94 | const getDashboard = yield* CloudWatch.GetDashboard(dashboard); |
| 95 | const getInsightRuleReport = yield* CloudWatch.GetInsightRuleReport(insightRule); |
| 96 | const getMetricData = yield* CloudWatch.GetMetricData(); |
| 97 | const getMetricStatistics = yield* CloudWatch.GetMetricStatistics(); |
| 98 | const getMetricWidgetImage = yield* CloudWatch.GetMetricWidgetImage(); |
| 99 | const listAlarmMuteRules = yield* CloudWatch.ListAlarmMuteRules(); |
| 100 | const listDashboards = yield* CloudWatch.ListDashboards(); |
| 101 | const listManagedInsightRules = yield* CloudWatch.ListManagedInsightRules(); |
| 102 | const listMetricStreams = yield* CloudWatch.ListMetricStreams(); |
| 103 | const listMetrics = yield* CloudWatch.ListMetrics(); |
| 104 | const listTagsForResource = yield* CloudWatch.ListTagsForResource(alarm); |
| 105 | const putMetricData = yield* CloudWatch.PutMetricData(); |
| 106 | const setAlarmState = yield* CloudWatch.SetAlarmState(alarm); |
| 107 | |
| 108 | // Physical identifiers, bound into the runtime environment so routes can |
| 109 | // echo them back for containment assertions in the test. |
| 110 | const AlarmName = yield* alarm.alarmName; |
| 111 | const AlarmArn = yield* alarm.alarmArn; |
| 112 | const DashboardName = yield* dashboard.dashboardName; |
| 113 | const RuleName = yield* insightRule.ruleName; |
| 114 | |
| 115 | return { |
| 116 | fetch: Effect.gen(function* () { |
| 117 | const request = yield* HttpServerRequest; |
| 118 | const url = new URL(request.originalUrl); |
| 119 | const pathname = url.pathname; |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/health") { |
| 122 | return HttpServerResponse.text("ok"); |
| 123 | } |
| 124 | |
| 125 | // ── Metrics ──────────────────────────────────────────────────────── |
| 126 | |
| 127 | if (request.method === "POST" && pathname === "/put-metric-data") { |
| 128 | const result = yield* putMetricData({ |
| 129 | Namespace: METRIC_NAMESPACE, |
| 130 | MetricData: [ |
| 131 | { |
| 132 | MetricName: METRIC_NAME, |
| 133 | Value: 1, |
| 134 | Unit: "Count", |
| 135 | }, |
| 136 | ], |
| 137 | }); |
| 138 | return yield* HttpServerResponse.json({ ok: true, result }); |
| 139 | } |
| 140 | |
| 141 | if (request.method === "GET" && pathname === "/list-metrics") { |
| 142 | const result = yield* listMetrics({ Namespace: METRIC_NAMESPACE }); |
| 143 | return yield* HttpServerResponse.json({ |
| 144 | metrics: result.Metrics ?? [], |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | if (request.method === "GET" && pathname === "/get-metric-data") { |
| 149 | const now = yield* Effect.sync(() => Date.now()); |
| 150 | const result = yield* getMetricData({ |
| 151 | StartTime: new Date(now - 3_600_000), |
| 152 | EndTime: new Date(now), |
| 153 | MetricDataQueries: [ |
| 154 | { |
| 155 | Id: "m1", |
| 156 | MetricStat: { |
| 157 | Metric: { |
| 158 | Namespace: METRIC_NAMESPACE, |
| 159 | MetricName: METRIC_NAME, |
| 160 | }, |
| 161 | Period: 60, |
| 162 | Stat: "Sum", |
| 163 | }, |
| 164 | }, |
| 165 | ], |
| 166 | }); |
| 167 | return yield* HttpServerResponse.json({ |
| 168 | results: result.MetricDataResults ?? [], |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | if (request.method === "GET" && pathname === "/get-metric-statistics") { |
| 173 | const now = yield* Effect.sync(() => Date.now()); |
| 174 | const result = yield* getMetricStatistics({ |
| 175 | Namespace: METRIC_NAMESPACE, |
| 176 | MetricName: METRIC_NAME, |
| 177 | StartTime: new Date(now - 3_600_000), |
| 178 | EndTime: new Date(now), |
| 179 | Period: 60, |
| 180 | Statistics: ["Sum"], |
| 181 | }); |
| 182 | return yield* HttpServerResponse.json({ |
| 183 | label: result.Label, |
| 184 | datapoints: result.Datapoints ?? [], |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | if (request.method === "GET" && pathname === "/get-metric-widget-image") { |
| 189 | const result = yield* getMetricWidgetImage({ |
| 190 | MetricWidget: JSON.stringify({ |
| 191 | metrics: [[METRIC_NAMESPACE, METRIC_NAME]], |
| 192 | width: 300, |
| 193 | height: 200, |
| 194 | start: "-PT1H", |
| 195 | }), |
| 196 | }); |
| 197 | return yield* HttpServerResponse.json({ |
| 198 | bytes: result.MetricWidgetImage?.byteLength ?? 0, |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | // ── Alarms ───────────────────────────────────────────────────────── |
| 203 | |
| 204 | if (request.method === "GET" && pathname === "/describe-alarms") { |
| 205 | const result = yield* describeAlarms(); |
| 206 | return yield* HttpServerResponse.json({ |
| 207 | alarmName: yield* AlarmName, |
| 208 | alarms: (result.MetricAlarms ?? []).map((a) => a.AlarmName), |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | if (request.method === "GET" && pathname === "/describe-alarms-for-metric") { |
| 213 | const result = yield* describeAlarmsForMetric({ |
| 214 | Namespace: METRIC_NAMESPACE, |
| 215 | MetricName: METRIC_NAME, |
| 216 | Statistic: "Sum", |
| 217 | Period: 60, |
| 218 | }); |
| 219 | return yield* HttpServerResponse.json({ |
| 220 | alarmName: yield* AlarmName, |
| 221 | alarms: (result.MetricAlarms ?? []).map((a) => a.AlarmName), |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | if (request.method === "GET" && pathname === "/describe-alarm-history") { |
| 226 | const result = yield* describeAlarmHistory({ |
| 227 | AlarmName: yield* AlarmName, |
| 228 | MaxRecords: 10, |
| 229 | }); |
| 230 | return yield* HttpServerResponse.json({ |
| 231 | items: result.AlarmHistoryItems ?? [], |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | if (request.method === "GET" && pathname === "/describe-alarm-contributors") { |
| 236 | // Contributor data only exists for alarms with contributor-enabled |
| 237 | // metric math; a plain metric alarm returns the typed |
| 238 | // ValidationException (observed live: empty message) or |
| 239 | // ResourceNotFoundException. Any outcome proves binding + IAM. |
| 240 | const result = yield* describeAlarmContributors().pipe( |
| 241 | Effect.map((r) => ({ |
| 242 | ok: true as const, |
| 243 | contributors: r.AlarmContributors, |
| 244 | })), |
| 245 | Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) => |
| 246 | Effect.succeed({ |
| 247 | ok: false as const, |
| 248 | error: e._tag, |
| 249 | }), |
| 250 | ), |
| 251 | ); |
| 252 | return yield* HttpServerResponse.json(result); |
| 253 | } |
| 254 | |
| 255 | if (request.method === "POST" && pathname === "/set-alarm-state") { |
| 256 | const result = yield* setAlarmState({ |
| 257 | StateValue: "OK", |
| 258 | StateReason: "alchemy-cloudwatch-bindings-test", |
| 259 | }); |
| 260 | return yield* HttpServerResponse.json({ ok: true, result }); |
| 261 | } |
| 262 | |
| 263 | if (request.method === "POST" && pathname === "/disable-alarm-actions") { |
| 264 | const result = yield* disableAlarmActions(); |
| 265 | return yield* HttpServerResponse.json({ ok: true, result }); |
| 266 | } |
| 267 | |
| 268 | if (request.method === "POST" && pathname === "/enable-alarm-actions") { |
| 269 | const result = yield* enableAlarmActions(); |
| 270 | return yield* HttpServerResponse.json({ ok: true, result }); |
| 271 | } |
| 272 | |
| 273 | if (request.method === "GET" && pathname === "/list-tags-for-resource") { |
| 274 | const result = yield* listTagsForResource(); |
| 275 | return yield* HttpServerResponse.json({ |
| 276 | tags: result.Tags ?? [], |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | // ── Dashboards ───────────────────────────────────────────────────── |
| 281 | |
| 282 | if (request.method === "GET" && pathname === "/get-dashboard") { |
| 283 | const result = yield* getDashboard(); |
| 284 | return yield* HttpServerResponse.json({ |
| 285 | dashboardName: yield* DashboardName, |
| 286 | name: result.DashboardName, |
| 287 | body: result.DashboardBody, |
| 288 | }); |
| 289 | } |
| 290 | |
| 291 | if (request.method === "GET" && pathname === "/list-dashboards") { |
| 292 | const result = yield* listDashboards(); |
| 293 | return yield* HttpServerResponse.json({ |
| 294 | dashboardName: yield* DashboardName, |
| 295 | entries: (result.DashboardEntries ?? []).map((e) => e.DashboardName), |
| 296 | }); |
| 297 | } |
| 298 | |
| 299 | // ── Contributor Insights ─────────────────────────────────────────── |
| 300 | |
| 301 | if (request.method === "GET" && pathname === "/describe-insight-rules") { |
| 302 | const result = yield* describeInsightRules(); |
| 303 | return yield* HttpServerResponse.json({ |
| 304 | ruleName: yield* RuleName, |
| 305 | rules: (result.InsightRules ?? []).map((r) => r.Name), |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | if (request.method === "GET" && pathname === "/get-insight-rule-report") { |
| 310 | const now = yield* Effect.sync(() => Date.now()); |
| 311 | const result = yield* getInsightRuleReport({ |
| 312 | StartTime: new Date(now - 3_600_000), |
| 313 | EndTime: new Date(now), |
| 314 | Period: 300, |
| 315 | }); |
| 316 | return yield* HttpServerResponse.json({ |
| 317 | ok: true, |
| 318 | aggregationStatistic: result.AggregationStatistic, |
| 319 | contributors: result.Contributors ?? [], |
| 320 | }); |
| 321 | } |
| 322 | |
| 323 | if (request.method === "POST" && pathname === "/disable-insight-rules") { |
| 324 | const result = yield* disableInsightRules(); |
| 325 | return yield* HttpServerResponse.json({ |
| 326 | failures: result.Failures ?? [], |
| 327 | }); |
| 328 | } |
| 329 | |
| 330 | if (request.method === "POST" && pathname === "/enable-insight-rules") { |
| 331 | const result = yield* enableInsightRules(); |
| 332 | return yield* HttpServerResponse.json({ |
| 333 | failures: result.Failures ?? [], |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | if (request.method === "GET" && pathname === "/list-managed-insight-rules") { |
| 338 | // Managed Contributor Insights rules only exist for specific AWS |
| 339 | // resource types. We probe with the alarm ARN: a supported resource |
| 340 | // returns ManagedRules, an unsupported one returns the typed |
| 341 | // InvalidParameterValueException. Both outcomes prove binding + IAM. |
| 342 | const result = yield* listManagedInsightRules({ |
| 343 | ResourceARN: yield* AlarmArn, |
| 344 | }).pipe( |
| 345 | Effect.map((r) => ({ |
| 346 | ok: true as const, |
| 347 | rules: r.ManagedRules ?? [], |
| 348 | })), |
| 349 | Effect.catchTag("InvalidParameterValueException", () => |
| 350 | Effect.succeed({ |
| 351 | ok: false as const, |
| 352 | error: "InvalidParameterValueException", |
| 353 | }), |
| 354 | ), |
| 355 | ); |
| 356 | return yield* HttpServerResponse.json(result); |
| 357 | } |
| 358 | |
| 359 | // ── Anomaly detectors ────────────────────────────────────────────── |
| 360 | |
| 361 | if (request.method === "GET" && pathname === "/describe-anomaly-detectors") { |
| 362 | const result = yield* describeAnomalyDetectors({ |
| 363 | Namespace: METRIC_NAMESPACE, |
| 364 | }); |
| 365 | return yield* HttpServerResponse.json({ |
| 366 | detectors: result.AnomalyDetectors ?? [], |
| 367 | }); |
| 368 | } |
| 369 | |
| 370 | // ── Alarm mute rules / metric streams (list-only; see test notes) ── |
| 371 | |
| 372 | if (request.method === "GET" && pathname === "/list-alarm-mute-rules") { |
| 373 | const result = yield* listAlarmMuteRules(); |
| 374 | return yield* HttpServerResponse.json({ |
| 375 | summaries: result.AlarmMuteRuleSummaries ?? [], |
| 376 | }); |
| 377 | } |
| 378 | |
| 379 | if (request.method === "GET" && pathname === "/list-metric-streams") { |
| 380 | const result = yield* listMetricStreams(); |
| 381 | return yield* HttpServerResponse.json({ |
| 382 | entries: result.Entries ?? [], |
| 383 | }); |
| 384 | } |
| 385 | |
| 386 | return yield* HttpServerResponse.json( |
| 387 | { |
| 388 | error: "Not found", |
| 389 | method: request.method, |
| 390 | pathname, |
| 391 | }, |
| 392 | { status: 404 }, |
| 393 | ); |
| 394 | }).pipe(Effect.orDie), |
| 395 | }; |
| 396 | }).pipe( |
| 397 | Effect.provide( |
| 398 | Layer.mergeAll( |
| 399 | CloudWatch.DescribeAlarmContributorsHttp, |
| 400 | CloudWatch.DescribeAlarmHistoryHttp, |
| 401 | CloudWatch.DescribeAlarmsHttp, |
| 402 | CloudWatch.DescribeAlarmsForMetricHttp, |
| 403 | CloudWatch.DescribeAnomalyDetectorsHttp, |
| 404 | CloudWatch.DescribeInsightRulesHttp, |
| 405 | CloudWatch.DisableAlarmActionsHttp, |
| 406 | CloudWatch.DisableInsightRulesHttp, |
| 407 | CloudWatch.EnableAlarmActionsHttp, |
| 408 | CloudWatch.EnableInsightRulesHttp, |
| 409 | CloudWatch.GetDashboardHttp, |
| 410 | CloudWatch.GetInsightRuleReportHttp, |
| 411 | CloudWatch.GetMetricDataHttp, |
| 412 | CloudWatch.GetMetricStatisticsHttp, |
| 413 | CloudWatch.GetMetricWidgetImageHttp, |
| 414 | CloudWatch.ListAlarmMuteRulesHttp, |
| 415 | CloudWatch.ListDashboardsHttp, |
| 416 | CloudWatch.ListManagedInsightRulesHttp, |
| 417 | CloudWatch.ListMetricStreamsHttp, |
| 418 | CloudWatch.ListMetricsHttp, |
| 419 | CloudWatch.ListTagsForResourceHttp, |
| 420 | CloudWatch.PutMetricDataHttp, |
| 421 | CloudWatch.SetAlarmStateHttp, |
| 422 | ), |
| 423 | ), |
| 424 | ), |
| 425 | ); |