@@ -25,6 +25,8 @@ For example, `lambda.Function` objects have the `fn.metricErrors()` method, whic
25
25
represents the amount of errors reported by that Lambda function:
26
26
27
27
``` ts
28
+ declare const fn: lambda .Function ;
29
+
28
30
const errors = fn .metricErrors ();
29
31
```
30
32
@@ -37,7 +39,7 @@ For example:
37
39
38
40
``` ts
39
41
const hostedZone = new route53 .HostedZone (this , ' MyHostedZone' , { zoneName: " example.org" });
40
- const metric = new Metric ({
42
+ const metric = new cloudwatch . Metric ({
41
43
namespace: ' AWS/Route53' ,
42
44
metricName: ' DNSQueries' ,
43
45
dimensionsMap: {
@@ -52,7 +54,7 @@ If you want to reference a metric that is not yet exposed by an existing constru
52
54
you can instantiate a ` Metric ` object to represent it. For example:
53
55
54
56
``` ts
55
- const metric = new Metric ({
57
+ const metric = new cloudwatch . Metric ({
56
58
namespace: ' MyNamespace' ,
57
59
metricName: ' MyMetric' ,
58
60
dimensionsMap: {
@@ -67,11 +69,13 @@ Math expressions are supported by instantiating the `MathExpression` class.
67
69
For example, a math expression that sums two other metrics looks like this:
68
70
69
71
``` ts
70
- const allProblems = new MathExpression ({
71
- expression: " errors + faults" ,
72
+ declare const fn: lambda .Function ;
73
+
74
+ const allProblems = new cloudwatch .MathExpression ({
75
+ expression: " errors + throttles" ,
72
76
usingMetrics: {
73
- errors: myConstruct .metricErrors (),
74
- faults: myConstruct . metricFaults (),
77
+ errors: fn .metricErrors (),
78
+ faults: fn . metricThrottles (),
75
79
}
76
80
});
77
81
```
@@ -80,11 +84,14 @@ You can use `MathExpression` objects like any other metric, including using
80
84
them in other math expressions:
81
85
82
86
``` ts
83
- const problemPercentage = new MathExpression ({
87
+ declare const fn: lambda .Function ;
88
+ declare const allProblems: cloudwatch .MathExpression ;
89
+
90
+ const problemPercentage = new cloudwatch .MathExpression ({
84
91
expression: " (problems / invocations) * 100" ,
85
92
usingMetrics: {
86
93
problems: allProblems ,
87
- invocations: myConstruct .metricInvocations ()
94
+ invocations: fn .metricInvocations ()
88
95
}
89
96
});
90
97
```
@@ -96,7 +103,7 @@ search expression returns all CPUUtilization metrics that it finds, with the
96
103
graph showing the Average statistic with an aggregation period of 5 minutes:
97
104
98
105
``` ts
99
- const cpuUtilization = new MathExpression ({
106
+ const cpuUtilization = new cloudwatch . MathExpression ({
100
107
expression: " SEARCH('{AWS/EC2,InstanceId} MetricName=\" CPUUtilization\" ', 'Average', 300)"
101
108
});
102
109
```
@@ -120,6 +127,8 @@ the function or the period), you can do so by passing additional parameters
120
127
to the metric function call:
121
128
122
129
``` ts
130
+ declare const fn: lambda .Function ;
131
+
123
132
const minuteErrorRate = fn .metricErrors ({
124
133
statistic: ' avg' ,
125
134
period: Duration .minutes (1 ),
@@ -153,9 +162,10 @@ useful when embedding them in graphs, see below).
153
162
Alarms can be created on metrics in one of two ways. Either create an ` Alarm `
154
163
object, passing the ` Metric ` object to set the alarm on:
155
164
156
-
157
165
``` ts
158
- new Alarm (this , ' Alarm' , {
166
+ declare const fn: lambda .Function ;
167
+
168
+ new cloudwatch .Alarm (this , ' Alarm' , {
159
169
metric: fn .metricErrors (),
160
170
threshold: 100 ,
161
171
evaluationPeriods: 2 ,
@@ -165,6 +175,8 @@ new Alarm(this, 'Alarm', {
165
175
Alternatively, you can call ` metric.createAlarm() ` :
166
176
167
177
``` ts
178
+ declare const fn: lambda .Function ;
179
+
168
180
fn .metricErrors ().createAlarm (this , ' Alarm' , {
169
181
threshold: 100 ,
170
182
evaluationPeriods: 2 ,
@@ -188,33 +200,36 @@ an SNS topic when an alarm breaches, do the following:
188
200
189
201
``` ts
190
202
import * as cw_actions from ' @aws-cdk/aws-cloudwatch-actions' ;
203
+ declare const alarm: cloudwatch .Alarm ;
191
204
192
- // ...
193
- const topic = new sns .Topic (stack , ' Topic' );
194
- const alarm = new cloudwatch .Alarm (stack , ' Alarm' , { /* ... */ });
195
-
205
+ const topic = new sns .Topic (this , ' Topic' );
196
206
alarm .addAlarmAction (new cw_actions .SnsAction (topic ));
197
207
```
198
208
199
209
### Composite Alarms
200
210
201
- [ Composite Alarms] ( https://aws.amazon.com/about-aws/whats-new/2020/03/amazon-cloudwatch-now-allows-you-to-combine-multiple-alarms/ )
211
+ [ Composite Alarms] ( https://aws.amazon.com/about-aws/whats-new/2020/03/amazon-cloudwatch-now-allows-you-to-combine-multiple-alarms/ )
202
212
can be created from existing Alarm resources.
203
213
204
214
``` ts
205
- const alarmRule = AlarmRule .anyOf (
206
- AlarmRule .allOf (
207
- AlarmRule .anyOf (
215
+ declare const alarm1: cloudwatch .Alarm ;
216
+ declare const alarm2: cloudwatch .Alarm ;
217
+ declare const alarm3: cloudwatch .Alarm ;
218
+ declare const alarm4: cloudwatch .Alarm ;
219
+
220
+ const alarmRule = cloudwatch .AlarmRule .anyOf (
221
+ cloudwatch .AlarmRule .allOf (
222
+ cloudwatch .AlarmRule .anyOf (
208
223
alarm1 ,
209
- AlarmRule .fromAlarm (alarm2 , AlarmState .OK ),
224
+ cloudwatch . AlarmRule .fromAlarm (alarm2 , cloudwatch . AlarmState .OK ),
210
225
alarm3 ,
211
226
),
212
- AlarmRule .not (AlarmRule .fromAlarm (alarm4 , AlarmState .INSUFFICIENT_DATA )),
227
+ cloudwatch . AlarmRule .not (cloudwatch . AlarmRule .fromAlarm (alarm4 , cloudwatch . AlarmState .INSUFFICIENT_DATA )),
213
228
),
214
- AlarmRule .fromBoolean (false ),
229
+ cloudwatch . AlarmRule .fromBoolean (false ),
215
230
);
216
231
217
- new CompositeAlarm (this , ' MyAwesomeCompositeAlarm' , {
232
+ new cloudwatch . CompositeAlarm (this , ' MyAwesomeCompositeAlarm' , {
218
233
alarmRule ,
219
234
});
220
235
```
@@ -260,15 +275,19 @@ A graph widget can display any number of metrics on either the `left` or
260
275
` right ` vertical axis:
261
276
262
277
``` ts
263
- dashboard .addWidgets (new GraphWidget ({
278
+ declare const dashboard: cloudwatch .Dashboard ;
279
+ declare const executionCountMetric: cloudwatch .Metric ;
280
+ declare const errorCountMetric: cloudwatch .Metric ;
281
+
282
+ dashboard .addWidgets (new cloudwatch .GraphWidget ({
264
283
title: " Executions vs error rate" ,
265
284
266
285
left: [executionCountMetric ],
267
286
268
287
right: [errorCountMetric .with ({
269
288
statistic: " average" ,
270
289
label: " Error rate" ,
271
- color: Color .GREEN
290
+ color: cloudwatch . Color .GREEN
272
291
})]
273
292
}));
274
293
```
@@ -278,12 +297,13 @@ Using the methods `addLeftMetric()` and `addRightMetric()` you can add metrics t
278
297
Graph widgets can also display annotations attached to the left or the right y-axis.
279
298
280
299
``` ts
281
- dashboard .addWidgets (new GraphWidget ({
282
- // ...
300
+ declare const dashboard: cloudwatch .Dashboard ;
301
+
302
+ dashboard .addWidgets (new cloudwatch .GraphWidget ({
283
303
// ...
284
304
285
305
leftAnnotations: [
286
- { value: 1800 , label: Duration .minutes (30 ).toHumanString (), color: Color .RED , },
306
+ { value: 1800 , label: Duration .minutes (30 ).toHumanString (), color: cloudwatch . Color .RED , },
287
307
{ value: 3600 , label: ' 1 hour' , color: ' #2ca02c' , }
288
308
],
289
309
}));
@@ -292,19 +312,21 @@ dashboard.addWidgets(new GraphWidget({
292
312
The graph legend can be adjusted from the default position at bottom of the widget.
293
313
294
314
``` ts
295
- dashboard .addWidgets (new GraphWidget ({
296
- // ...
315
+ declare const dashboard: cloudwatch .Dashboard ;
316
+
317
+ dashboard .addWidgets (new cloudwatch .GraphWidget ({
297
318
// ...
298
319
299
- legendPosition: LegendPosition .RIGHT ,
320
+ legendPosition: cloudwatch . LegendPosition .RIGHT ,
300
321
}));
301
322
```
302
323
303
324
The graph can publish live data within the last minute that has not been fully aggregated.
304
325
305
326
``` ts
306
- dashboard .addWidgets (new GraphWidget ({
307
- // ...
327
+ declare const dashboard: cloudwatch .Dashboard ;
328
+
329
+ dashboard .addWidgets (new cloudwatch .GraphWidget ({
308
330
// ...
309
331
310
332
liveData: true ,
@@ -314,11 +336,12 @@ dashboard.addWidgets(new GraphWidget({
314
336
The graph view can be changed from default 'timeSeries' to 'bar' or 'pie'.
315
337
316
338
``` ts
317
- dashboard .addWidgets (new GraphWidget ({
318
- // ...
339
+ declare const dashboard: cloudwatch .Dashboard ;
340
+
341
+ dashboard .addWidgets (new cloudwatch .GraphWidget ({
319
342
// ...
320
343
321
- view: GraphWidgetView .BAR ,
344
+ view: cloudwatch . GraphWidgetView .BAR ,
322
345
}));
323
346
```
324
347
@@ -327,7 +350,10 @@ dashboard.addWidgets(new GraphWidget({
327
350
An alarm widget shows the graph and the alarm line of a single alarm:
328
351
329
352
``` ts
330
- dashboard .addWidgets (new AlarmWidget ({
353
+ declare const dashboard: cloudwatch .Dashboard ;
354
+ declare const errorAlarm: cloudwatch .Alarm ;
355
+
356
+ dashboard .addWidgets (new cloudwatch .AlarmWidget ({
331
357
title: " Errors" ,
332
358
alarm: errorAlarm ,
333
359
}));
@@ -339,17 +365,22 @@ A single-value widget shows the latest value of a set of metrics (as opposed
339
365
to a graph of the value over time):
340
366
341
367
``` ts
342
- dashboard .addWidgets (new SingleValueWidget ({
368
+ declare const dashboard: cloudwatch .Dashboard ;
369
+ declare const visitorCount: cloudwatch .Metric ;
370
+ declare const purchaseCount: cloudwatch .Metric ;
371
+
372
+ dashboard .addWidgets (new cloudwatch .SingleValueWidget ({
343
373
metrics: [visitorCount , purchaseCount ],
344
374
}));
345
375
```
346
376
347
377
Show as many digits as can fit, before rounding.
348
378
349
379
``` ts
350
- dashboard .addWidgets (new SingleValueWidget ({
351
- // ..
352
- // ..
380
+ declare const dashboard: cloudwatch .Dashboard ;
381
+
382
+ dashboard .addWidgets (new cloudwatch .SingleValueWidget ({
383
+ metrics: [ /* ... */ ],
353
384
354
385
fullPrecision: true ,
355
386
}));
@@ -361,7 +392,9 @@ A text widget shows an arbitrary piece of MarkDown. Use this to add explanations
361
392
to your dashboard:
362
393
363
394
``` ts
364
- dashboard .addWidgets (new TextWidget ({
395
+ declare const dashboard: cloudwatch .Dashboard ;
396
+
397
+ dashboard .addWidgets (new cloudwatch .TextWidget ({
365
398
markdown: ' # Key Performance Indicators'
366
399
}));
367
400
```
@@ -372,8 +405,11 @@ An alarm status widget displays instantly the status of any type of alarms and g
372
405
ability to aggregate one or more alarms together in a small surface.
373
406
374
407
``` ts
408
+ declare const dashboard: cloudwatch .Dashboard ;
409
+ declare const errorAlarm: cloudwatch .Alarm ;
410
+
375
411
dashboard .addWidgets (
376
- new AlarmStatusWidget ({
412
+ new cloudwatch . AlarmStatusWidget ({
377
413
alarms: [errorAlarm ],
378
414
})
379
415
);
@@ -384,9 +420,11 @@ dashboard.addWidgets(
384
420
A ` LogQueryWidget ` shows the results of a query from Logs Insights:
385
421
386
422
``` ts
387
- dashboard .addWidgets (new LogQueryWidget ({
423
+ declare const dashboard: cloudwatch .Dashboard ;
424
+
425
+ dashboard .addWidgets (new cloudwatch .LogQueryWidget ({
388
426
logGroupNames: [' my-log-group' ],
389
- view: LogQueryVisualizationType .TABLE ,
427
+ view: cloudwatch . LogQueryVisualizationType .TABLE ,
390
428
// The lines will be automatically combined using '\n|'.
391
429
queryLines: [
392
430
' fields @message' ,
0 commit comments