Skip to content

Commit 7fd2eea

Browse files
enverEnver Cicak
andauthored
feat: Added anomaly detection support (#36)
Co-authored-by: Enver Cicak <ecicak@eworklabs.io>
1 parent f90685e commit 7fd2eea

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

examples/lambda-metric-alarm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ No providers.
3131
| Name | Source | Version |
3232
|------|--------|---------|
3333
| <a name="module_alarm"></a> [alarm](#module\_alarm) | ../../modules/metric-alarm | n/a |
34+
| <a name="module_alarm_anomaly"></a> [alarm\_anomaly](#module\_alarm\_anomaly) | ../../modules/metric-alarm | n/a |
3435
| <a name="module_alarm_metric_query"></a> [alarm\_metric\_query](#module\_alarm\_metric\_query) | ../../modules/metric-alarm | n/a |
3536
| <a name="module_all_lambdas_errors_alarm"></a> [all\_lambdas\_errors\_alarm](#module\_all\_lambdas\_errors\_alarm) | ../../modules/metric-alarm | n/a |
3637
| <a name="module_aws_lambda_function1"></a> [aws\_lambda\_function1](#module\_aws\_lambda\_function1) | ../fixtures/aws_lambda_function | n/a |

examples/lambda-metric-alarm/main.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,44 @@ module "alarm_metric_query" {
106106
Secure = "maybe"
107107
}
108108
}
109+
110+
module "alarm_anomaly" {
111+
source = "../../modules/metric-alarm"
112+
113+
alarm_name = "lambda-invocations-anomaly-${module.aws_lambda_function2.random_id}"
114+
alarm_description = "Lambda invocations anomaly"
115+
comparison_operator = "LessThanLowerOrGreaterThanUpperThreshold"
116+
evaluation_periods = 1
117+
threshold_metric_id = "ad1"
118+
119+
metric_query = [{
120+
id = "ad1"
121+
122+
return_data = true
123+
expression = "ANOMALY_DETECTION_BAND(m1, 2)"
124+
label = "Invocations (expected)"
125+
return_data = "true"
126+
},
127+
{
128+
id = "m1"
129+
130+
metric = [{
131+
namespace = "AWS/Lambda"
132+
metric_name = "Invocations"
133+
period = 60
134+
stat = "Sum"
135+
unit = "Count"
136+
137+
dimensions = {
138+
FunctionName = module.aws_lambda_function2.lambda_function_name
139+
}
140+
}]
141+
return_data = "true"
142+
}]
143+
144+
alarm_actions = [module.aws_sns_topic.sns_topic_arn]
145+
146+
tags = {
147+
Secure = "maybe"
148+
}
149+
}

modules/metric-alarm/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ No modules.
4747
| <a name="input_period"></a> [period](#input\_period) | The period in seconds over which the specified statistic is applied. | `string` | `null` | no |
4848
| <a name="input_statistic"></a> [statistic](#input\_statistic) | The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum | `string` | `null` | no |
4949
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to all resources | `map(string)` | `{}` | no |
50-
| <a name="input_threshold"></a> [threshold](#input\_threshold) | The value against which the specified statistic is compared. | `number` | n/a | yes |
50+
| <a name="input_threshold"></a> [threshold](#input\_threshold) | The value against which the specified statistic is compared. | `number` | `null` | no |
51+
| <a name="input_threshold_metric_id"></a> [threshold\_metric\_id](#input\_threshold\_metric\_id) | If this is an alarm based on an anomaly detection model, make this value match the ID of the ANOMALY\_DETECTION\_BAND function. | `string` | `null` | no |
5152
| <a name="input_treat_missing_data"></a> [treat\_missing\_data](#input\_treat\_missing\_data) | Sets how this alarm is to handle missing data points. The following values are supported: missing, ignore, breaching and notBreaching. | `string` | `"missing"` | no |
5253
| <a name="input_unit"></a> [unit](#input\_unit) | The unit for the alarm's associated metric. | `string` | `null` | no |
5354

modules/metric-alarm/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ resource "aws_cloudwatch_metric_alarm" "this" {
5050
}
5151
}
5252
}
53+
threshold_metric_id = var.threshold_metric_id
5354

5455
tags = var.tags
5556
}

modules/metric-alarm/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ variable "evaluation_periods" {
2828
variable "threshold" {
2929
description = "The value against which the specified statistic is compared."
3030
type = number
31+
default = null
32+
}
33+
34+
variable "threshold_metric_id" {
35+
description = "If this is an alarm based on an anomaly detection model, make this value match the ID of the ANOMALY_DETECTION_BAND function."
36+
type = string
37+
default = null
3138
}
3239

3340
variable "unit" {

0 commit comments

Comments
 (0)