The aws-cloudwatch package supports setting CloudWatch alarms on CloudWatch metrics. So the first thing you need is a metric. You can use a predefined metric or you can create your own.
Many AWS Construct Library modules let you set an alarm on an existing metric by passing the metric's name to a convenience method on an instance of an object that has metrics. For example, given an Amazon SQS queue, you can get the metric ApproximateNumberOfMessagesVisible from the queue's metric() method.
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
const metric = queue.metric("ApproximateNumberOfMessagesVisible");
metric = queue.metric("ApproximateNumberOfMessagesVisible")
Metric metric = queue.metric("ApproximateNumberOfMessagesVisible");
var metric = queue.Metric("ApproximateNumberOfMessagesVisible");
Create your own metric as follows, where the namespace value should be something like AWS/SQS for an Amazon SQS queue. You also need to specify your metric's name and dimension.
const metric = new cloudwatch.Metric({
namespace: 'MyNamespace',
metricName: 'MyMetric',
dimensions: { MyDimension: 'MyDimensionValue' }
});
const metric = new cloudwatch.Metric({
namespace: 'MyNamespace',
metricName: 'MyMetric',
dimensions: { MyDimension: 'MyDimensionValue' }
});
metric = cloudwatch.Metric(
namespace="MyNamespace",
metric_name="MyMetric",
dimensions=dict(MyDimension="MyDimensionValue")
)
Metric metric = Metric.Builder.create()
.namespace("MyNamespace")
.metricName("MyMetric")
.dimensionsMap(java.util.Map.of( // Java 9 or later
"MyDimension", "MyDimensionValue"))
.build();
var metric = new Metric(this, "Metric", new MetricProps
{
Namespace = "MyNamespace",
MetricName = "MyMetric",
Dimensions = new Dictionary<string, object>
{
{ "MyDimension", "MyDimensionValue" }
}
});
Once you have a metric, either an existing one or one you defined, you can create an alarm. In this example, the alarm is raised when there are more than 100 of your metric in two of the last three evaluation periods. You can use comparisons such as less-than in your alarms via the comparisonOperator
property; greater-than-or-equal-to is the AWS CDK default, so we don't need to specify it.
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
metric: metric,
threshold: 100,
evaluationPeriods: 3,
datapointsToAlarm: 2,
});
const alarm = new cloudwatch.Alarm(this, 'Alarm', {
metric: metric,
threshold: 100,
evaluationPeriods: 3,
datapointsToAlarm: 2
});
alarm = cloudwatch.Alarm(self, "Alarm",
metric=metric,
threshold=100,
evaluation_periods=3,
datapoints_to_alarm=2
)
import software.amazon.awscdk.services.cloudwatch.Alarm;
import software.amazon.awscdk.services.cloudwatch.Metric;
Alarm alarm = Alarm.Builder.create(this, "Alarm")
.metric(metric)
.threshold(100)
.evaluationPeriods(3)
.datapointsToAlarm(2).build();
var alarm = new Alarm(this, "Alarm", new AlarmProps
{
Metric = metric,
Threshold = 100,
EvaluationPeriods = 3,
DatapointsToAlarm = 2
});
An alternative way to create an alarm is using the metric's createAlarm() method, which takes essentially the same properties as the Alarm
constructor; you just don't need to pass in the metric, since it's already known.
metric.createAlarm(this, 'Alarm', {
threshold: 100,
evaluationPeriods: 3,
datapointsToAlarm: 2,
});
metric.createAlarm(this, 'Alarm', {
threshold: 100,
evaluationPeriods: 3,
datapointsToAlarm: 2,
});
metric.create_alarm(self, "Alarm",
threshold=100,
evaluation_periods=3,
datapoints_to_alarm=2
)
metric.createAlarm(this, "Alarm", new CreateAlarmOptions.Builder()
.threshold(100)
.evaluationPeriods(3)
.datapointsToAlarm(2)
.build());
metric.CreateAlarm(this, "Alarm", new CreateAlarmOptions
{
Threshold = 100,
EvaluationPeriods = 3,
DatapointsToAlarm = 2
});