Skip to content
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

fix(cloudwatch): cloudwatch ec2 alarm action with multiple dimension results in error #29364

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class Alarm extends AlarmBase {
if (ec2ActionsRegexp.test(actionArn)) {
// Check per-instance metric
const metricConfig = this.metric.toMetricConfig();
if (metricConfig.metricStat?.dimensions?.length != 1 || metricConfig.metricStat?.dimensions![0].name != 'InstanceId') {
if (metricConfig.metricStat?.dimensions?.length != 1 || !metricConfig.metricStat?.dimensions?.some(dimension => dimension.name === 'InstanceId')) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just stumbled upon this PR and I think that there should be && operator instead of ||. We want to throw the exception when there are multiple dimensions AND none of them is InstanceId. Is that right?

throw new Error(`EC2 alarm actions requires an EC2 Per-Instance Metric. (${JSON.stringify(metricConfig)} does not have an 'InstanceId' dimension)`);
}
}
Expand Down
63 changes: 62 additions & 1 deletion packages/aws-cdk-lib/aws-cloudwatch/test/alarm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Construct } from 'constructs';
import { Match, Template, Annotations } from '../../assertions';
import { Duration, Stack } from '../../core';
import { Ec2Action, Ec2InstanceAction } from '../../aws-cloudwatch-actions/lib';
import { Duration, Stack, App } from '../../core';
import { ENABLE_PARTITION_LITERALS } from '../../cx-api';
import { Alarm, IAlarm, IAlarmAction, Metric, MathExpression, IMetric, Stats } from '../lib';

const testMetric = new Metric({
Expand Down Expand Up @@ -232,6 +234,65 @@ describe('Alarm', () => {
});
});

test('EC2 alarm actions with InstanceId dimension', () => {
// GIVEN
const app = new App({ context: { [ ENABLE_PARTITION_LITERALS]: true } });
const stack = new Stack(app, 'EC2AlarmStack', { env: { region: 'us-west-2', account: '123456789012' } });

// WHEN
const metric = new Metric({
namespace: 'CWAgent',
metricName: 'disk_used_percent',
dimensionsMap: {
InstanceId: 'instance-id',
},
period: Duration.minutes(5),
statistic: 'Average',
});

const sev3Alarm = new Alarm(stack, 'DISK_USED_PERCENT_SEV3', {
alarmName: 'DISK_USED_PERCENT_SEV3',
actionsEnabled: true,
metric: metric,
threshold: 1,
evaluationPeriods: 1,
});

expect(() => {
sev3Alarm.addAlarmAction(new Ec2Action(Ec2InstanceAction.REBOOT));
}).not.toThrow();
});

test('EC2 alarm actions without InstanceId dimension', () => {
// GIVEN
const app = new App({ context: { [ ENABLE_PARTITION_LITERALS]: true } });
const stack = new Stack(app, 'EC2AlarmStack', { env: { region: 'us-west-2', account: '123456789012' } });

// WHEN
const metric = new Metric({
namespace: 'CWAgent',
metricName: 'disk_used_percent',
dimensionsMap: {
ImageId: 'image-id',
InstanceType: 't2.micro',
},
period: Duration.minutes(5),
statistic: 'Average',
});

const sev3Alarm = new Alarm(stack, 'DISK_USED_PERCENT_SEV3', {
alarmName: 'DISK_USED_PERCENT_SEV3',
actionsEnabled: true,
metric: metric,
threshold: 1,
evaluationPeriods: 1,
});

expect(() => {
sev3Alarm.addAlarmAction(new Ec2Action(Ec2InstanceAction.REBOOT));
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric/);
});

test('can use percentile string to make alarm', () => {
// GIVEN
const stack = new Stack();
Expand Down
Loading