-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CloudWatch: Math expression in alarm doesn't respect period #32221
Comments
Reproducible using below customer provided code: import * as cdk from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
export class CdktestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
alarmName: 'TestAlarm',
actionsEnabled: false,
datapointsToAlarm: 3,
evaluationPeriods: 5,
metric: new cloudwatch.MathExpression({
period: cdk.Duration.seconds(60),
expression: 'TIME_SERIES(0) + ((INSIGHT_RULE_METRIC("' +
'testrulename' +
'", "MaxContributorValue") / m1) * 100)',
usingMetrics: {
"m1": new cloudwatch.Metric({
namespace: "AWS/ApplicationELB",
metricName: "RequestCount",
dimensionsMap: {
"LoadBalancer": "test"
},
statistic: "Sum",
period: cdk.Duration.seconds(60)
})
}
}),
threshold: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD
});
}
} Running Resources:
MyAlarm696658B6:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: false
AlarmName: TestAlarm
ComparisonOperator: GreaterThanOrEqualToThreshold
DatapointsToAlarm: 3
EvaluationPeriods: 5
Metrics:
- Expression: TIME_SERIES(0) + ((INSIGHT_RULE_METRIC("testrulename", "MaxContributorValue") / m1) * 100)
Id: expr_1
- Id: m1
MetricStat:
Metric:
Dimensions:
- Name: LoadBalancer
Value: test
MetricName: RequestCount
Namespace: AWS/ApplicationELB
Period: 60
Stat: Sum
ReturnData: false
Threshold: 1
Metadata:
aws:cdk:path: CdktestStack/MyAlarm/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/yXGQQ6DIBAAwLd4h60a03Bt+gP7ALNdaEQREhbKwfB3o55meuieCtoGC0vSq3T2C/snIa0CC0/kQtYFE82wvxzGTbx//ko9NxoOOZKpwgdtYOHHv1PQtzA0C1srY/bJbgbG2wMtdOBxbQAAAA==
Metadata:
aws:cdk:path: CdktestStack/CDKMetadata/Default
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] Running
Looks like in PR #7644, it allowed to set export class CdktestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new cloudwatch.Alarm(this, 'Alarm', {
alarmName: 'TestAlarm',
actionsEnabled: false,
datapointsToAlarm: 3,
evaluationPeriods: 5,
metric: new cloudwatch.MathExpression({
expression: 'INSIGHT_RULE_METRIC("SomeId", UniqueContributors)',
usingMetrics: {},
period: cdk.Duration.seconds(60)
}),
threshold: 1,
});
}
} synthesizes to below CloudFormation template: Resources:
Alarm7103F465:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: false
AlarmName: TestAlarm
ComparisonOperator: GreaterThanOrEqualToThreshold
DatapointsToAlarm: 3
EvaluationPeriods: 5
Metrics:
- Expression: INSIGHT_RULE_METRIC("SomeId", UniqueContributors)
Id: expr_1
Period: 60
Threshold: 1
Metadata:
aws:cdk:path: CdktestStack/Alarm/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/yXGQQ6DIBAAwLd4h60a03Bt+gP7ALNdaEQREhbKwfB3o55meuieCtoGC0vSq3T2C/snIa0CC0/kQtYFE82wvxzGTbx//ko9NxoOOZKpwgdtYOHHv1PQtzA0C1srY/bJbgbG2wMtdOBxbQAAAA==
Metadata:
aws:cdk:path: CdktestStack/CDKMetadata/Default
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] @hakenmt You could try to use raw property override as a workaround (demonstrated below): import * as cdk from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
export class CdktestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
alarmName: 'TestAlarm',
actionsEnabled: false,
datapointsToAlarm: 3,
evaluationPeriods: 5,
metric: new cloudwatch.MathExpression({
period: cdk.Duration.seconds(60),
expression: 'TIME_SERIES(0) + ((INSIGHT_RULE_METRIC("' +
'testrulename' +
'", "MaxContributorValue") / m1) * 100)',
usingMetrics: {
"m1": new cloudwatch.Metric({
namespace: "AWS/ApplicationELB",
metricName: "RequestCount",
dimensionsMap: {
"LoadBalancer": "test"
},
statistic: "Sum",
period: cdk.Duration.seconds(60)
})
}
}),
threshold: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD
});
const cfnAlarm = alarm.node.defaultChild as cloudwatch.CfnAlarm;
cfnAlarm.addPropertyOverride('Metrics.0.Period', 60);
}
} |
Thanks, yes, ended up with that workaround after submitting this bug report. |
Describe the bug
I'm defining an alarm as follows:
This produces CFN as follows:
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.
Regression Issue
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
The text was updated successfully, but these errors were encountered: