Define Cloudwatch with CDK
Workflow
- Define cloudwatch metrics
- Define cloudwatch alarms
- Define cloudwatch carnaval monitor
- Construct alarms in the app
- Define dashboard with metrics
Define cloudwatch metrics
There are 2 ways to define metrics in CDK:
- use existing metric
- Define a new metric
Example to use existing metric:
const dlqMessageCountMetric = dlq.metric("ApproximateNumberOfMessagesVisible");
Example to define a new metric:
function createMessageCountMetric(dlsQueueName: string) {
return new Metric({
namespace: 'AWS/SQS',
metricName: 'ApproximateNumberOfMessagesVisible',
dimensions: {
'QueueName': `${dlsQueueName}`
}
});
}
dlqMessageCountMetric = createMessageCountMetric('myDLQ');
Define cloudwatch alarm
There are 2 ways to define an alarm too:
- Create a new alarm
- Use Metric.createAlarm function.
With defined metric, define the alarm, set the threshold, period and datapoints:
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
metric: dlqMessageCountMetric,
threshold: 5,
evaluationPeriods: 60,
datapointsToAlarm: 3,
});
An alternative way to create an alarm is using the metric’s createAlarm() method, which takes essentially the same properties as the Alarm constructor;
const alarm = dlqMessageCountMetric.createAlarm(this, 'Alarm', {
threshold: 5,
evaluationPeriods: 60,
datapointsToAlarm: 3,
});
Define Carnaval monitor on alarm
The best way to use alarms is to hook up with a notification system, such as Carnaval
const monitor = new CarnavalMonitor(this, `Sev3-CarnavalMonitor-DLQ`, {
name: `DLQ-CarnavalMonitor`,
owner: owner,
region: region,
alarms: [CloudWatchAlarmMonitor.fromCloudWatchAlarm(alarm)],
ticketNotification: {
cti: cti,
severity: Severity.SEV3,
details: alarm.alarmName
}
});
Deploy the monitor along with alarms
Define dashboard with metrics
In addition to alarms, we can define dashboard to visualize the system operation and monitoring.
First of all, need to import cloudwatch package:
import {Dashboard, GraphWidget, TextWidget} from "@aws-cdk/aws-cloudwatch";
import {Construct} from "@aws-cdk/core";
Secondly, create widgets.
const widgetTitle = new TextWidget({
markdown: "MyWidget",
width: 24,
height: 1
});
const widgetInstance = new GraphWidget({
title: title,
width: 8,
height: 6,
left: [dlqMessageCountMetric],
right: [...],
stacked: false
});
Define Dashboard instance and add widgets by addWidgets or in the constructor:
const dashboard = new Dashboard(scope, `MyDashboard`, {
dashboardName: `MyDashboard`, //name, must be unique in the account
start: '-P3H', //Start time
widgets: [[widgetTitle], [widgetInstance]]
});
Reference
- https://docs.aws.amazon.com/cdk/latest/guide/how_to_set_cw_alarm.html
- https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudwatch-readme.html
- https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudwatch.Dashboard.html
- https://docs.aws.amazon.com/cloudwatch/index.html