Skip to content

Commit

Permalink
Cleanup comments and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
scorbiere committed Aug 27, 2024
1 parent 2b9571b commit 850f892
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,3 @@ const app = new App();
new IntegTest(app, 'AnomalyDetectionAlarmIntegTest', {
testCases: [new AnomalyDetectionAlarmTestStack(app, 'AnomalyDetectionAlarmTestStack')],
});

// app.synth();

/**
* Integration test for anomaly detection alarms
*
* SYNTHESIS VERIFICATION:
* 1. Check for the presence of ANOMALY_DETECTION_BAND in the template
* - The Metrics property should contain an item with Expression: "ANOMALY_DETECTION_BAND(m1, 2)"
* 2. Verify that ThresholdMetricId is set and Threshold is not present
* - The Alarm resource should have a ThresholdMetricId property set to "expr_1"
* - The Alarm resource should not have a Threshold property
* 3. Ensure both metrics have ReturnData set to true
* - Both items in the Metrics array should have "ReturnData": true
*/
57 changes: 18 additions & 39 deletions packages/aws-cdk-lib/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@

## Anomaly Detection Alarms

CloudWatch supports creating alarms based on anomaly detection. You can create an anomaly detection alarm using the `createAnomalyDetectionAlarm` method on a `Metric` object. Here's an example:

```typescript
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';

const metric = new cloudwatch.Metric({
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: { InstanceId: 'i-1234567890abcdef0' },
});

const anomalyAlarm = metric.createAnomalyDetectionAlarm(this, 'AnomalyAlarm', {
evaluationPeriods: 3,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_UPPER_THRESHOLD,
});
```

This creates an alarm that triggers when the metric exceeds the upper threshold of the anomaly detection band for 3 consecutive evaluation periods. The anomaly detection band is automatically calculated by CloudWatch based on the metric's historical data.

Note that anomaly detection alarms don't require a threshold value, as the threshold is dynamically determined by the anomaly detection algorithm. The `createAnomalyDetectionAlarm` method handles the necessary configuration, including setting the `thresholdMetricId` to the appropriate value.
# Amazon CloudWatch Construct Library

## Anomaly Detection Alarms

To create an alarm based on anomaly detection, you can use the `createAnomalyDetectionAlarm` method on a `Metric` object:

```typescript
const queue = new sqs.Queue(this, 'Queue');
const alarm = queue.metricNumberOfMessagesSent().createAnomalyDetectionAlarm(this, 'AnomalyAlarm', {
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD,
});
```

This method automatically sets up the necessary ANOMALY_DETECTION_BAND expression and configures the alarm correctly for anomaly detection.

For more information on anomaly detection in CloudWatch, see the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html).


## Metric objects

Expand Down Expand Up @@ -426,6 +387,24 @@ only supports filtering by `unit` for Alarms, not in Dashboard graphs.
Please see the following GitHub issue for a discussion on real unit
calculations in CDK: https://github.com/aws/aws-cdk/issues/5595

## Anomaly Detection Alarms

To create an alarm based on anomaly detection, you can use the `createAnomalyDetectionAlarm` method on a `Metric` or `MathExpression` object:

```typescript
const queue = new sqs.Queue(this, 'Queue');
const alarm = queue.metricNumberOfMessagesSent().createAnomalyDetectionAlarm(this, 'AnomalyAlarm', {
bounds: 2,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD,
});
```

This method automatically sets up the necessary ANOMALY_DETECTION_BAND expression and configures the alarm correctly for anomaly detection.
Note that anomaly detection alarms don't require a threshold value, as the threshold is dynamically determined by the anomaly detection algorithm. The `createAnomalyDetectionAlarm` method handles the necessary configuration, including setting the `thresholdMetricId` to the appropriate value.
For more information on anomaly detection in CloudWatch, see the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html).


## Dashboards

Dashboards are set of Widgets stored server-side which can be accessed quickly
Expand Down
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 @@ -145,7 +145,7 @@ export class Alarm extends AlarmBase {
}

/**
* Determine whether this alarm uses an anomaly detection operator.
* Determine whether this operator is an anomaly detection operator.
*
* @param operator the comparison operator for the alarm.
* @returns true if the operator is an anomaly detection operator, false otherwise.
Expand Down

0 comments on commit 850f892

Please sign in to comment.