aws / aws/aws-cdk

CloudWatch: Math expression in alarm doesn't respect period

Open
#32,221 2 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-cloudwatch bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

I'm defining an alarm as follows:

```typescript
let latencyImpactAlarm: Alarm = new Alarm(
this,
val + "LatencyImpact",
{
alarmName: val + "-node-high-latency-impact",
actionsEnabled: false,
datapointsToAlarm: 3,
evaluationPeriods: 5,
metric: new MathExpression({
period: Duration.seconds(60),
expression: 'TIME_SERIES(0) + ((INSIGHT_RULE_METRIC("' +
latencyRule.ruleName +
'", "MaxContributorValue") / m1) * 100)',
usingMetrics: {
"m1": new Metric({
namespace: "AWS/ApplicationELB",
metricName: "RequestCount",
dimensionsMap: {
"LoadBalancer": value
},
statistic: "Sum",
period: Duration.seconds(60)
})
}
}),
threshold: .1,
comparisonOperator:
ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
},
);
```
This produces CFN as follows:

```json
"Metrics": [
{
"Expression": "TIME_SERIES(0) + ((INSIGHT_RULE_METRIC(\"HighLatencyRequestsPerNode-app-multi--ALBAE-7sFFT5bABDxM-2fa0f6aeb46f1279\", \"MaxContributorValue\") / m1) * 100)",
"Id": "expr_1"
},
{
"Id": "m1",
"MetricStat": {
"Metric": {
"Dimensions": [
{
"Name": "LoadBalancer",
"Value": "app/multi--ALBAE-7sFFT5bABDxM/2fa0f6aeb46f1279"
}
],
"MetricName": "RequestCount",
"Namespace": "AWS/ApplicationELB"
},
"Period": 60,
"Stat": "Sum"
},
"ReturnData": false
}
],
```

When deployed, this causes an error: `Resource handler returned message: "Error in expression 'expr_1': Parameter Query Period is invalid. Please specify a Period. `

Even though the period is set, it is not included in the synth'd CFN template. If you add in the period manually, the resource successfully deploys.
```json
{
"Expression": "TIME_SERIES(0) + ((INSIGHT_RULE_METRIC(\"HighLatencyRequestsPerNode-app-multi--ALBAE-7sFFT5bABDxM-2fa0f6aeb46f1279\", \"MaxContributorValue\") / m1) * 100)",
"Id": "expr_1",
"Period": 60
},
```

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Version

_No response_

### Expected Behavior

I expect the period field to be included in the resulting CFN.

### Current Behavior

It does not.

### Reproduction Steps

Provided above.

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.138.0

### Framework Version

_No response_

### Node.js Version

20

### OS

darwin

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the MathExpression and Alarm constructs in the reproduction, then inspect the synthesized CloudFormation metrics and how the expression period is represented. Done means the generated expression metric includes Period 60 and the provided alarm configuration deploys successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.