|
1 | 1 | import { Construct } from 'constructs';
|
2 | 2 | import { Match, Template, Annotations } from '../../assertions';
|
3 |
| -import { Duration, Stack } from '../../core'; |
| 3 | +import { Ec2Action, Ec2InstanceAction } from '../../aws-cloudwatch-actions/lib'; |
| 4 | +import { Duration, Stack, App } from '../../core'; |
| 5 | +import { ENABLE_PARTITION_LITERALS } from '../../cx-api'; |
4 | 6 | import { Alarm, IAlarm, IAlarmAction, Metric, MathExpression, IMetric, Stats } from '../lib';
|
5 | 7 |
|
6 | 8 | const testMetric = new Metric({
|
@@ -232,6 +234,65 @@ describe('Alarm', () => {
|
232 | 234 | });
|
233 | 235 | });
|
234 | 236 |
|
| 237 | + test('EC2 alarm actions with InstanceId dimension', () => { |
| 238 | + // GIVEN |
| 239 | + const app = new App({ context: { [ ENABLE_PARTITION_LITERALS]: true } }); |
| 240 | + const stack = new Stack(app, 'EC2AlarmStack', { env: { region: 'us-west-2', account: '123456789012' } }); |
| 241 | + |
| 242 | + // WHEN |
| 243 | + const metric = new Metric({ |
| 244 | + namespace: 'CWAgent', |
| 245 | + metricName: 'disk_used_percent', |
| 246 | + dimensionsMap: { |
| 247 | + InstanceId: 'instance-id', |
| 248 | + }, |
| 249 | + period: Duration.minutes(5), |
| 250 | + statistic: 'Average', |
| 251 | + }); |
| 252 | + |
| 253 | + const sev3Alarm = new Alarm(stack, 'DISK_USED_PERCENT_SEV3', { |
| 254 | + alarmName: 'DISK_USED_PERCENT_SEV3', |
| 255 | + actionsEnabled: true, |
| 256 | + metric: metric, |
| 257 | + threshold: 1, |
| 258 | + evaluationPeriods: 1, |
| 259 | + }); |
| 260 | + |
| 261 | + expect(() => { |
| 262 | + sev3Alarm.addAlarmAction(new Ec2Action(Ec2InstanceAction.REBOOT)); |
| 263 | + }).not.toThrow(); |
| 264 | + }); |
| 265 | + |
| 266 | + test('EC2 alarm actions without InstanceId dimension', () => { |
| 267 | + // GIVEN |
| 268 | + const app = new App({ context: { [ ENABLE_PARTITION_LITERALS]: true } }); |
| 269 | + const stack = new Stack(app, 'EC2AlarmStack', { env: { region: 'us-west-2', account: '123456789012' } }); |
| 270 | + |
| 271 | + // WHEN |
| 272 | + const metric = new Metric({ |
| 273 | + namespace: 'CWAgent', |
| 274 | + metricName: 'disk_used_percent', |
| 275 | + dimensionsMap: { |
| 276 | + ImageId: 'image-id', |
| 277 | + InstanceType: 't2.micro', |
| 278 | + }, |
| 279 | + period: Duration.minutes(5), |
| 280 | + statistic: 'Average', |
| 281 | + }); |
| 282 | + |
| 283 | + const sev3Alarm = new Alarm(stack, 'DISK_USED_PERCENT_SEV3', { |
| 284 | + alarmName: 'DISK_USED_PERCENT_SEV3', |
| 285 | + actionsEnabled: true, |
| 286 | + metric: metric, |
| 287 | + threshold: 1, |
| 288 | + evaluationPeriods: 1, |
| 289 | + }); |
| 290 | + |
| 291 | + expect(() => { |
| 292 | + sev3Alarm.addAlarmAction(new Ec2Action(Ec2InstanceAction.REBOOT)); |
| 293 | + }).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric/); |
| 294 | + }); |
| 295 | + |
235 | 296 | test('can use percentile string to make alarm', () => {
|
236 | 297 | // GIVEN
|
237 | 298 | const stack = new Stack();
|
|
0 commit comments