Skip to content

Commit

Permalink
fix(aws-cloudwatch): fix for space in alarm name in alarms for compos… (
Browse files Browse the repository at this point in the history
aws#13963)

Issue: aws#13953

This change is to allow creation of composite alarms from alarms having spaces in their alarm name. 
Ref https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Composite_Alarm.html

For more details please check above mentioned issue.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
somanshusingla authored and john-tipper committed May 10, 2021
1 parent a3ae4fd commit 8bcc257
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/alarm-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class AlarmBase extends Resource implements IAlarm {
* AlarmRule indicating ALARM state for Alarm.
*/
public renderAlarmRule(): string {
return `ALARM(${this.alarmArn})`;
return `ALARM("${this.alarmArn}")`;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/alarm-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class AlarmRule {
public static fromAlarm(alarm: IAlarm, alarmState: AlarmState): IAlarmRule {
return new class implements IAlarmRule {
public renderAlarmRule(): string {
return `${alarmState}(${alarm.alarmArn})`;
return `${alarmState}("${alarm.alarmArn}")`;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
"Threshold": 100000
}
},
"Alarm548383B2F": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 3,
"AlarmName": "Alarm with space in name",
"MetricName": "Metric",
"Namespace": "CDK/Test",
"Period": 300,
"Statistic": "Average",
"Threshold": 100000
}
},
"CompositeAlarmF4C3D082": {
"Type": "AWS::CloudWatch::CompositeAlarm",
"Properties": {
Expand All @@ -56,35 +69,42 @@
"Fn::Join": [
"",
[
"(((ALARM(",
"(((ALARM(\"",
{
"Fn::GetAtt": [
"Alarm1F9009D71",
"Arn"
]
},
") OR OK(",
"\") OR OK(\"",
{
"Fn::GetAtt": [
"Alarm2A7122E13",
"Arn"
]
},
") OR ALARM(",
"\") OR ALARM(\"",
{
"Fn::GetAtt": [
"Alarm32341D8D9",
"Arn"
]
},
")) AND (NOT (INSUFFICIENT_DATA(",
"\") OR ALARM(\"",
{
"Fn::GetAtt": [
"Alarm548383B2F",
"Arn"
]
},
"\")) AND (NOT (INSUFFICIENT_DATA(\"",
{
"Fn::GetAtt": [
"Alarm4671832C8",
"Arn"
]
},
")))) OR FALSE)"
"\")))) OR FALSE)"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ class CompositeAlarmIntegrationTest extends Stack {
evaluationPeriods: 3,
});

const alarm5 = new Alarm(this, 'Alarm5', {
alarmName: 'Alarm with space in name',
metric: testMetric,
threshold: 100000,
evaluationPeriods: 3,
});

const alarmRule = AlarmRule.anyOf(
AlarmRule.allOf(
AlarmRule.anyOf(
alarm1,
AlarmRule.fromAlarm(alarm2, AlarmState.OK),
alarm3,
alarm5,
),
AlarmRule.not(AlarmRule.fromAlarm(alarm4, AlarmState.INSUFFICIENT_DATA)),
),
Expand Down
25 changes: 20 additions & 5 deletions packages/@aws-cdk/aws-cloudwatch/test/test.composite-alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ export = {
evaluationPeriods: 3,
});

const alarm5 = new Alarm(stack, 'Alarm5', {
alarmName: 'Alarm with space in name',
metric: testMetric,
threshold: 100000,
evaluationPeriods: 3,
});

const alarmRule = AlarmRule.anyOf(
AlarmRule.allOf(
AlarmRule.anyOf(
alarm1,
AlarmRule.fromAlarm(alarm2, AlarmState.OK),
alarm3,
alarm5,
),
AlarmRule.not(AlarmRule.fromAlarm(alarm4, AlarmState.INSUFFICIENT_DATA)),
),
Expand All @@ -58,35 +66,42 @@ export = {
'Fn::Join': [
'',
[
'(((ALARM(',
'(((ALARM("',
{
'Fn::GetAtt': [
'Alarm1F9009D71',
'Arn',
],
},
') OR OK(',
'") OR OK("',
{
'Fn::GetAtt': [
'Alarm2A7122E13',
'Arn',
],
},
') OR ALARM(',
'") OR ALARM("',
{
'Fn::GetAtt': [
'Alarm32341D8D9',
'Arn',
],
},
')) AND (NOT (INSUFFICIENT_DATA(',
'") OR ALARM("',
{
'Fn::GetAtt': [
'Alarm548383B2F',
'Arn',
],
},
'")) AND (NOT (INSUFFICIENT_DATA("',
{
'Fn::GetAtt': [
'Alarm4671832C8',
'Arn',
],
},
')))) OR FALSE)',
'")))) OR FALSE)',
],
],
},
Expand Down

0 comments on commit 8bcc257

Please sign in to comment.