gotchajavascriptModerate
CloudWatch alarms not triggering due to missing data points being treated as OK
Viewed 0 times
cloudwatch alarmmissing datatreat missing datanotBreachingINSUFFICIENT_DATAevaluation periodmetric gaps
Problem
A CloudWatch alarm stays in OK state during a Lambda error burst because the metric has gaps (MISSING data points) and the alarm's treat-missing-data is set to notBreaching. When Lambda errors occur sparsely, the alarm never fires.
Solution
Set treat-missing-data to breaching for availability-type alarms (e.g., success rate). Use missing as a signal that something is wrong. For metrics that naturally have gaps (e.g., error counts when there are no errors), use notBreaching. Use composite alarms to combine multiple conditions.
// CDK: treat missing data as breaching for uptime alarms
new cloudwatch.Alarm(this, 'ErrorAlarm', {
metric: fn.metricErrors({ period: Duration.minutes(1) }),
threshold: 1,
evaluationPeriods: 1,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
alarmDescription: 'Lambda function errors',
});Why
CloudWatch metrics are only emitted when data exists. Lambda emits an Errors metric only when there are errors. If you set alarmDescription on the zero-count baseline and missing data as OK, you miss the error window entirely.
Gotchas
- Period too short + sparse invocations = missing data by design — use longer evaluation periods
- CloudWatch math expressions can use FILL to substitute 0 for missing values before applying thresholds
- Anomaly detection alarms handle sparse metrics better than threshold alarms for variable workloads
- CloudWatch alarms have a minimum resolution of 10 seconds (high-resolution) or 60 seconds (standard)
Code Snippets
Using FILL() in CloudWatch metric math to handle missing data
// Use FILL in metric math to replace missing values with 0
const filledErrors = new cloudwatch.MathExpression({
expression: 'FILL(errors, 0)',
usingMetrics: { errors: fn.metricErrors({ period: Duration.minutes(1) }) },
});
new cloudwatch.Alarm(this, 'FilledErrorAlarm', {
metric: filledErrors,
threshold: 1,
evaluationPeriods: 5,
datapointsToAlarm: 3,
});Context
Setting up CloudWatch alarms for Lambda, API Gateway, or other services with sparse metrics
Revisions (0)
No revisions yet.