Skip to content

Alarm

Source: src/AWS/CloudWatch/Alarm.ts

A CloudWatch metric alarm — watches a single metric (or metric-math expression) and transitions between OK, ALARM, and INSUFFICIENT_DATA, optionally firing actions on state change.

Threshold Alarm

const alarm = yield* Alarm("HighErrors", {
MetricName: "Errors",
Namespace: "AWS/Lambda",
Statistic: "Sum",
Period: 60,
EvaluationPeriods: 1,
Threshold: 1,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
});

Alarm on a Lambda Function’s Errors

const fn = yield* MyFunction;
const alarm = yield* Alarm("FnErrors", {
MetricName: "Errors",
Namespace: "AWS/Lambda",
Dimensions: [{ Name: "FunctionName", Value: fn.functionName }],
Statistic: "Sum",
Period: 60,
EvaluationPeriods: 1,
Threshold: 1,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
TreatMissingData: "notBreaching",
});
// init — bind the alarm to the function (see DescribeAlarms)
const describeAlarms = yield* AWS.CloudWatch.DescribeAlarms(alarm);
// runtime
const result = yield* describeAlarms();
const state = result.MetricAlarms?.[0]?.StateValue;