From 033535bab16ea172a5e90a17fffdec96e1f34cc9 Mon Sep 17 00:00:00 2001 From: Julia Aghamyan Date: Fri, 23 Jun 2023 16:15:54 +0400 Subject: [PATCH 1/3] feat(DMVP-expression-alerts): Add new expression alert --- modules/alerts/README.md | 2 + modules/alerts/main.tf | 19 +++++++ .../alerts/tests/expression-alert/0-setup.tf | 20 ++++++++ .../tests/expression-alert/1-example.tf | 50 +++++++++++++++++++ .../alerts/tests/expression-alert/2-assert.tf | 9 ++++ .../alerts/tests/expression-alert/README.md | 37 ++++++++++++++ modules/alerts/variables.tf | 5 ++ 7 files changed, 142 insertions(+) create mode 100644 modules/alerts/tests/expression-alert/0-setup.tf create mode 100644 modules/alerts/tests/expression-alert/1-example.tf create mode 100644 modules/alerts/tests/expression-alert/2-assert.tf create mode 100644 modules/alerts/tests/expression-alert/README.md diff --git a/modules/alerts/README.md b/modules/alerts/README.md index 8eca39a..9995f32 100644 --- a/modules/alerts/README.md +++ b/modules/alerts/README.md @@ -98,6 +98,7 @@ module "alerts" { | Name | Source | Version | |------|--------|---------| +| [cloudwatch\_expression-alarm](#module\_cloudwatch\_expression-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 | | [cloudwatch\_log-based-metric-alarm](#module\_cloudwatch\_log-based-metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 | | [cloudwatch\_metric-alarm](#module\_cloudwatch\_metric-alarm) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 | | [cloudwatch\_metric-alarm\_with\_anomalydetection](#module\_cloudwatch\_metric-alarm\_with\_anomalydetection) | terraform-aws-modules/cloudwatch/aws//modules/metric-alarm | 4.3.0 | @@ -119,6 +120,7 @@ module "alerts" { | [enable\_alarm\_actions](#input\_enable\_alarm\_actions) | Enable alarm actions alert | `bool` | `true` | no | | [enable\_insufficient\_data\_actions](#input\_enable\_insufficient\_data\_actions) | Enable insufficient data actions alert | `bool` | `true` | no | | [enable\_ok\_actions](#input\_enable\_ok\_actions) | Enable ok actions alert | `bool` | `true` | no | +| [expression\_alert](#input\_expression\_alert) | Add multiple alerts and add expression alerts. | `any` | n/a | yes | | [health\_checks](#input\_health\_checks) | Allows to create route53 health checks and alarms on them | `list(any)` | `[]` | no | | [sns\_topic](#input\_sns\_topic) | The name of aws sns topic use as target for alarm actions | `string` | `"cloudwatch-alerts"` | no | diff --git a/modules/alerts/main.tf b/modules/alerts/main.tf index fc57334..7cb5548 100644 --- a/modules/alerts/main.tf +++ b/modules/alerts/main.tf @@ -171,3 +171,22 @@ module "external_health_check-alarms" { ok_actions = local.ok_actions insufficient_data_actions = local.alarm_actions } + +module "cloudwatch_expression-alarm" { + source = "terraform-aws-modules/cloudwatch/aws//modules/metric-alarm" + version = "4.3.0" + + for_each = var.expression_alert + + alarm_name = each.key + alarm_description = lookup(each.value, "description", null) + comparison_operator = local.comparison_operators[each.value.equation] + threshold = each.value.threshold + evaluation_periods = 1 + + metric_query = each.value.metrics + + alarm_actions = var.enable_alarm_actions ? local.alarm_actions : null + ok_actions = var.enable_ok_actions ? local.ok_actions : null + insufficient_data_actions = var.enable_insufficient_data_actions ? local.alarm_actions : null +} diff --git a/modules/alerts/tests/expression-alert/0-setup.tf b/modules/alerts/tests/expression-alert/0-setup.tf new file mode 100644 index 0000000..8b85ae6 --- /dev/null +++ b/modules/alerts/tests/expression-alert/0-setup.tf @@ -0,0 +1,20 @@ +terraform { + required_providers { + test = { + source = "terraform.io/builtin/test" + } + + aws = { + source = "hashicorp/aws" + version = "~> 4.33" + } + } +} + +provider "aws" { + region = "eu-central-1" +} + +resource "aws_sns_topic" "tets_topic_for_alarm_actions" { + name = "tets-topic-for-alarm-actions" +} diff --git a/modules/alerts/tests/expression-alert/1-example.tf b/modules/alerts/tests/expression-alert/1-example.tf new file mode 100644 index 0000000..3d2f27e --- /dev/null +++ b/modules/alerts/tests/expression-alert/1-example.tf @@ -0,0 +1,50 @@ +module "expression_alert" { + source = "../../" + + + sns_topic = aws_sns_topic.tets_topic_for_alarm_actions.name + expression_alert = { + "excl_5xx" = { + equation = "lt" + threshold = 90 + metrics = [ + { + id = "m5x" + period = 0 + return_data = false + # metric = [] + metric = [{ + dimensions = { "LoadBalancer" = "app/alb-test/803783" } + metric_name = "HTTPCode_Target_5XX_Count" + namespace = "AWS/ApplicationELB" + period = 300 + stat = "Average" + } + ] + }, + { + id = "mTotal" + period = 0 + return_data = false + + metric = [{ + dimensions = { "LoadBalancer" = "app/alb-test/803783" } + metric_name = "RequestCount" + namespace = "AWS/ApplicationELB" + period = 300 + stat = "Average" + }] + }, + { + expression = "100*(mTotal-m5x)/mTotal" + id = "e1" + label = "Expression1" + period = 0 + return_data = true + } + ] + } + } + enable_insufficient_data_actions = false + enable_ok_actions = false +} diff --git a/modules/alerts/tests/expression-alert/2-assert.tf b/modules/alerts/tests/expression-alert/2-assert.tf new file mode 100644 index 0000000..dfb1b11 --- /dev/null +++ b/modules/alerts/tests/expression-alert/2-assert.tf @@ -0,0 +1,9 @@ +resource "test_assertions" "dummy" { + component = "monitoring-modules-alerts" + + equal "scheme" { + description = "As module does not have any output and data just make sure the case runs. Probably can be thrown away." + got = "all good" + want = "all good" + } +} diff --git a/modules/alerts/tests/expression-alert/README.md b/modules/alerts/tests/expression-alert/README.md new file mode 100644 index 0000000..86f8db2 --- /dev/null +++ b/modules/alerts/tests/expression-alert/README.md @@ -0,0 +1,37 @@ +# expression-alert + + +## Requirements + +| Name | Version | +|------|---------| +| [aws](#requirement\_aws) | ~> 4.33 | + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | ~> 4.33 | +| [test](#provider\_test) | n/a | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [expression\_alert](#module\_expression\_alert) | ../../ | n/a | + +## Resources + +| Name | Type | +|------|------| +| [aws_sns_topic.tets_topic_for_alarm_actions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource | +| test_assertions.dummy | resource | + +## Inputs + +No inputs. + +## Outputs + +No outputs. + diff --git a/modules/alerts/variables.tf b/modules/alerts/variables.tf index f42c5fa..7e43f1d 100644 --- a/modules/alerts/variables.tf +++ b/modules/alerts/variables.tf @@ -70,3 +70,8 @@ variable "enable_alarm_actions" { default = true description = "Enable alarm actions alert" } + +variable "expression_alert" { + type = any + description = "Add multiple alerts and add expression alerts." +} From 81c90b161fdcf10fdaa0e6885a8f461d05dcb58d Mon Sep 17 00:00:00 2001 From: Julia Aghamyan Date: Fri, 23 Jun 2023 16:19:15 +0400 Subject: [PATCH 2/3] feat(DMVP-expression-alerts): Add new expression alert --- modules/alerts/README.md | 2 +- modules/alerts/variables.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/alerts/README.md b/modules/alerts/README.md index 9995f32..8571271 100644 --- a/modules/alerts/README.md +++ b/modules/alerts/README.md @@ -120,7 +120,7 @@ module "alerts" { | [enable\_alarm\_actions](#input\_enable\_alarm\_actions) | Enable alarm actions alert | `bool` | `true` | no | | [enable\_insufficient\_data\_actions](#input\_enable\_insufficient\_data\_actions) | Enable insufficient data actions alert | `bool` | `true` | no | | [enable\_ok\_actions](#input\_enable\_ok\_actions) | Enable ok actions alert | `bool` | `true` | no | -| [expression\_alert](#input\_expression\_alert) | Add multiple alerts and add expression alerts. | `any` | n/a | yes | +| [expression\_alert](#input\_expression\_alert) | Add multiple metrics in one alert and add expression. | `any` | n/a | yes | | [health\_checks](#input\_health\_checks) | Allows to create route53 health checks and alarms on them | `list(any)` | `[]` | no | | [sns\_topic](#input\_sns\_topic) | The name of aws sns topic use as target for alarm actions | `string` | `"cloudwatch-alerts"` | no | diff --git a/modules/alerts/variables.tf b/modules/alerts/variables.tf index 7e43f1d..333a21a 100644 --- a/modules/alerts/variables.tf +++ b/modules/alerts/variables.tf @@ -73,5 +73,5 @@ variable "enable_alarm_actions" { variable "expression_alert" { type = any - description = "Add multiple alerts and add expression alerts." + description = "Add multiple metrics in one alert and add expression." } From 0f3865b5fa55eb9882837a24968b2bbe300a03aa Mon Sep 17 00:00:00 2001 From: Julia Aghamyan Date: Fri, 23 Jun 2023 17:44:37 +0400 Subject: [PATCH 3/3] feat(DMVP-expression-alerts): Add new expression alert --- modules/alerts/README.md | 2 +- modules/alerts/tests/expression-alert/README.md | 2 +- modules/alerts/variables.tf | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/alerts/README.md b/modules/alerts/README.md index 8571271..b67e6d5 100644 --- a/modules/alerts/README.md +++ b/modules/alerts/README.md @@ -120,7 +120,7 @@ module "alerts" { | [enable\_alarm\_actions](#input\_enable\_alarm\_actions) | Enable alarm actions alert | `bool` | `true` | no | | [enable\_insufficient\_data\_actions](#input\_enable\_insufficient\_data\_actions) | Enable insufficient data actions alert | `bool` | `true` | no | | [enable\_ok\_actions](#input\_enable\_ok\_actions) | Enable ok actions alert | `bool` | `true` | no | -| [expression\_alert](#input\_expression\_alert) | Add multiple metrics in one alert and add expression. | `any` | n/a | yes | +| [expression\_alert](#input\_expression\_alert) | Add multiple metrics in one alert and add expression. | `any` | `{}` | no | | [health\_checks](#input\_health\_checks) | Allows to create route53 health checks and alarms on them | `list(any)` | `[]` | no | | [sns\_topic](#input\_sns\_topic) | The name of aws sns topic use as target for alarm actions | `string` | `"cloudwatch-alerts"` | no | diff --git a/modules/alerts/tests/expression-alert/README.md b/modules/alerts/tests/expression-alert/README.md index 86f8db2..dbc7698 100644 --- a/modules/alerts/tests/expression-alert/README.md +++ b/modules/alerts/tests/expression-alert/README.md @@ -11,7 +11,7 @@ | Name | Version | |------|---------| -| [aws](#provider\_aws) | ~> 4.33 | +| [aws](#provider\_aws) | 4.67.0 | | [test](#provider\_test) | n/a | ## Modules diff --git a/modules/alerts/variables.tf b/modules/alerts/variables.tf index 333a21a..77bcc90 100644 --- a/modules/alerts/variables.tf +++ b/modules/alerts/variables.tf @@ -73,5 +73,6 @@ variable "enable_alarm_actions" { variable "expression_alert" { type = any + default = {} description = "Add multiple metrics in one alert and add expression." }