Add alerting thresholds to monitoring. (#254)

This commit is contained in:
Robert Fairburn authored and GitHub committed 2026-06-19 12:17:55 -05:00
1 parent 98cd54a428
commit ba70e93f65
4 files changed
+176 -23

No files matched your search

+30
View File
@@ -146,6 +146,36 @@ module "monitoring" {
}
```
# Configurable Alert Thresholds
All CloudWatch alarm thresholds, periods, and evaluation periods can be overridden via the `alert_thresholds` object. Each alarm type is optional — omit any field to use the module default.
| Field | Alarm | Default threshold | Default period | Default evaluation periods |
|---|---|---|---|---|
| `rds_cpu` | RDS CPU Utilization | 80 | 300s | 1 |
| `redis_cpu` | Redis CPU Utilization | 70 | 300s | 1 |
| `redis_cpu_engine` | Redis Engine CPU Utilization | 25 | 300s | 1 |
| `redis_memory` | Redis Database Memory % | 80 | 300s | 1 |
| `acm_cert_expiry` | ACM Certificate Expiry | 30 days | 86400s | 1 |
| `alb_healthyhosts` | ALB Healthy Host Count | 1 | 60s | 1 |
Example — raise the RDS CPU threshold and require 3 consecutive 5-minute periods before firing (15 minutes total):
```hcl
module "monitoring" {
# ...
alert_thresholds = {
rds_cpu = {
threshold = 90
period = 300
evaluation_periods = 3
}
}
}
```
Anomaly detection alarms (`redis_current_connections`, `redis_replication_lag`, `target_response_time`) are not included — they use CloudWatch anomaly detection bands and do not have a simple threshold/period/evaluation_periods structure.
# SNS topic ARNs map
Valid targets for `sns_topic_arns_map`:
+36 -5
View File
@@ -146,6 +146,36 @@ module "monitoring" {
}
```
# Configurable Alert Thresholds
All CloudWatch alarm thresholds, periods, and evaluation periods can be overridden via the `alert_thresholds` object. Each alarm type is optional — omit any field to use the module default.
| Field | Alarm | Default threshold | Default period | Default evaluation periods |
|---|---|---|---|---|
| `rds_cpu` | RDS CPU Utilization | 80 | 300s | 1 |
| `redis_cpu` | Redis CPU Utilization | 70 | 300s | 1 |
| `redis_cpu_engine` | Redis Engine CPU Utilization | 25 | 300s | 1 |
| `redis_memory` | Redis Database Memory % | 80 | 300s | 1 |
| `acm_cert_expiry` | ACM Certificate Expiry | 30 days | 86400s | 1 |
| `alb_healthyhosts` | ALB Healthy Host Count | 1 | 60s | 1 |
Example — raise the RDS CPU threshold and require 3 consecutive 5-minute periods before firing (15 minutes total):
```hcl
module "monitoring" {
# ...
alert_thresholds = {
rds_cpu = {
threshold = 90
period = 300
evaluation_periods = 3
}
}
}
```
Anomaly detection alarms (`redis_current_connections`, `redis_replication_lag`, `target_response_time`) are not included — they use CloudWatch anomaly detection bands and do not have a simple threshold/period/evaluation\_periods structure.
# SNS topic ARNs map
Valid targets for `sns_topic_arns_map`:
@@ -196,14 +226,14 @@ Deprecated (typo) aliases are still accepted for backwards compatibility:
## Requirements
| Name | Version |
|------|---------|
| ---- | ------- |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.12.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 6.37.0 |
## Providers
| Name | Version |
|------|---------|
| ---- | ------- |
| <a name="provider_archive"></a> [archive](#provider\_archive) | 2.7.1 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.39.0 |
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.4 |
@@ -215,7 +245,7 @@ No modules.
## Resources
| Name | Type |
|------|------|
| ---- | ---- |
| [aws_cloudwatch_event_rule.cron_monitoring_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
| [aws_cloudwatch_event_target.cron_monitoring_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
| [aws_cloudwatch_log_group.cron_monitoring_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
@@ -255,9 +285,10 @@ No modules.
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| ---- | ----------- | ---- | ------- | :------: |
| <a name="input_acm_certificate_arn"></a> [acm\_certificate\_arn](#input\_acm\_certificate\_arn) | n/a | `string` | `null` | no |
| <a name="input_albs"></a> [albs](#input\_albs) | n/a | <pre>list(object({<br/> name = string<br/> arn_suffix = string<br/> target_group_name = string<br/> target_group_arn_suffix = string<br/> min_containers = optional(string, 1)<br/> ecs_service_name = string<br/> alert_thresholds = optional(<br/> object({<br/> HTTPCode_ELB_5XX_Count = object({<br/> period = number<br/> threshold = number<br/> })<br/> HTTPCode_Target_5XX_Count = object({<br/> period = number<br/> threshold = number<br/> })<br/> }),<br/> {<br/> HTTPCode_ELB_5XX_Count = {<br/> period = 120<br/> threshold = 0<br/> },<br/> HTTPCode_Target_5XX_Count = {<br/> period = 120<br/> threshold = 0<br/> }<br/> }<br/> )<br/> }))</pre> | `[]` | no |
| <a name="input_alert_thresholds"></a> [alert\_thresholds](#input\_alert\_thresholds) | CloudWatch alarm threshold overrides. Each alarm type is optional; omitted fields use the defaults below. | <pre>object({<br/> rds_cpu = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 80<br/> period = 300<br/> evaluation_periods = 1<br/> })<br/> redis_cpu = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 70<br/> period = 300<br/> evaluation_periods = 1<br/> })<br/> redis_cpu_engine = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 25<br/> period = 300<br/> evaluation_periods = 1<br/> })<br/> redis_memory = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 80<br/> period = 300<br/> evaluation_periods = 1<br/> })<br/> acm_cert_expiry = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 30<br/> period = 86400<br/> evaluation_periods = 1<br/> })<br/> alb_healthyhosts = optional(object({<br/> threshold = number<br/> period = number<br/> evaluation_periods = number<br/> }), {<br/> threshold = 1<br/> period = 60<br/> evaluation_periods = 1<br/> })<br/> })</pre> | <pre>{<br/> "acm_cert_expiry": {<br/> "evaluation_periods": 1,<br/> "period": 86400,<br/> "threshold": 30<br/> },<br/> "alb_healthyhosts": {<br/> "evaluation_periods": 1,<br/> "period": 60,<br/> "threshold": 1<br/> },<br/> "rds_cpu": {<br/> "evaluation_periods": 1,<br/> "period": 300,<br/> "threshold": 80<br/> },<br/> "redis_cpu": {<br/> "evaluation_periods": 1,<br/> "period": 300,<br/> "threshold": 70<br/> },<br/> "redis_cpu_engine": {<br/> "evaluation_periods": 1,<br/> "period": 300,<br/> "threshold": 25<br/> },<br/> "redis_memory": {<br/> "evaluation_periods": 1,<br/> "period": 300,<br/> "threshold": 80<br/> }<br/>}</pre> | no |
| <a name="input_cron_monitoring"></a> [cron\_monitoring](#input\_cron\_monitoring) | n/a | <pre>object({<br/> mysql_host = string<br/> mysql_database = string<br/> mysql_user = string<br/> mysql_password_secret_name = string<br/> mysql_password_secret_kms_key_arn = optional(string, null)<br/> mysql_tls_config = optional(string, "true")<br/> vpc_id = string<br/> subnet_ids = list(string)<br/> rds_security_group_id = string<br/> delay_tolerance = string<br/> run_interval = string<br/> log_retention_in_days = optional(number, 7)<br/> ignore_list = optional(list(string), [])<br/> lambda_kms = optional(object({<br/> cmk_enabled = optional(bool, false)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-cron-monitoring")<br/> kms_base_policy = optional(list(object({<br/> sid = string<br/> effect = string<br/> principals = object({<br/> type = string<br/> identifiers = list(string)<br/> })<br/> actions = list(string)<br/> resources = list(string)<br/> conditions = optional(list(object({<br/> test = string<br/> variable = string<br/> values = list(string)<br/> })), [])<br/> })), null)<br/> extra_kms_policies = optional(list(any), [])<br/> }), {<br/> cmk_enabled = false<br/> kms_key_arn = null<br/> kms_alias = "fleet-cron-monitoring"<br/> kms_base_policy = null<br/> extra_kms_policies = []<br/> })<br/> })</pre> | `null` | no |
| <a name="input_customer_prefix"></a> [customer\_prefix](#input\_customer\_prefix) | n/a | `string` | `"fleet"` | no |
| <a name="input_default_sns_topic_arns"></a> [default\_sns\_topic\_arns](#input\_default\_sns\_topic\_arns) | n/a | `list(string)` | `[]` | no |
@@ -270,6 +301,6 @@ No modules.
## Outputs
| Name | Description |
|------|-------------|
| ---- | ----------- |
| <a name="output_cron_monitoring_lambda_arn"></a> [cron\_monitoring\_lambda\_arn](#output\_cron\_monitoring\_lambda\_arn) | n/a |
| <a name="output_cron_monitoring_lambda_role_arn"></a> [cron\_monitoring\_lambda\_role\_arn](#output\_cron\_monitoring\_lambda\_role\_arn) | n/a |
+18 -18
View File
@@ -3,12 +3,12 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_too_high" {
for_each = toset(var.mysql_cluster_members)
alarm_name = "rds_cpu_utilization_too_high-${var.customer_prefix}-${each.key}"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
evaluation_periods = tostring(var.alert_thresholds.rds_cpu.evaluation_periods)
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = "300"
period = tostring(var.alert_thresholds.rds_cpu.period)
statistic = "Average"
threshold = 80
threshold = var.alert_thresholds.rds_cpu.threshold
alarm_description = "Average database CPU utilization over last 5 minutes too high"
alarm_actions = lookup(var.sns_topic_arns_map, "rds_cpu_utilization_too_high", lookup(var.sns_topic_arns_map, "rds_cpu_untilizaton_too_high", var.default_sns_topic_arns))
ok_actions = lookup(var.sns_topic_arns_map, "rds_cpu_utilization_too_high", lookup(var.sns_topic_arns_map, "rds_cpu_untilizaton_too_high", var.default_sns_topic_arns))
@@ -154,12 +154,12 @@ resource "aws_cloudwatch_metric_alarm" "alb_healthyhosts" {
for_each = local.alb_map
alarm_name = "backend-healthyhosts-${var.customer_prefix}-${each.value.name}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
evaluation_periods = tostring(var.alert_thresholds.alb_healthyhosts.evaluation_periods)
metric_name = "HealthyHostCount"
namespace = "AWS/ApplicationELB"
period = "60"
period = tostring(var.alert_thresholds.alb_healthyhosts.period)
statistic = "Minimum"
threshold = each.value.min_containers
threshold = max(each.value.min_containers, var.alert_thresholds.alb_healthyhosts.threshold)
alarm_description = "This alarm indicates the number of Healthy Fleet hosts is lower than expected. Please investigate the load balancer \"${each.value.name}\" or the target group \"${each.value.target_group_name}\" and the fleet backend service \"${each.value.ecs_service_name}\""
actions_enabled = "true"
alarm_actions = lookup(var.sns_topic_arns_map, "alb_healthyhosts", lookup(var.sns_topic_arns_map, "alb_helthyhosts", var.default_sns_topic_arns))
@@ -240,15 +240,15 @@ resource "aws_cloudwatch_metric_alarm" "redis_cpu" {
alarm_name = "redis-cpu-utilization-${each.key}-${var.customer_prefix}"
alarm_description = "Redis cluster CPU utilization node ${each.key}"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
evaluation_periods = tostring(var.alert_thresholds.redis_cpu.evaluation_periods)
metric_name = "CPUUtilization"
namespace = "AWS/ElastiCache"
period = "300"
period = tostring(var.alert_thresholds.redis_cpu.period)
statistic = "Average"
alarm_actions = lookup(var.sns_topic_arns_map, "redis_cpu_utilization", var.default_sns_topic_arns)
ok_actions = lookup(var.sns_topic_arns_map, "redis_cpu_utilization", var.default_sns_topic_arns)
threshold = "70"
threshold = tostring(var.alert_thresholds.redis_cpu.threshold)
dimensions = {
CacheClusterId = each.key
@@ -261,15 +261,15 @@ resource "aws_cloudwatch_metric_alarm" "redis_cpu_engine_utilization" {
alarm_name = "redis-cpu-engine-utilization-${each.key}-${var.customer_prefix}"
alarm_description = "Redis cluster CPU Engine utilization node ${each.key}"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
evaluation_periods = tostring(var.alert_thresholds.redis_cpu_engine.evaluation_periods)
metric_name = "EngineCPUUtilization"
namespace = "AWS/ElastiCache"
period = "300"
period = tostring(var.alert_thresholds.redis_cpu_engine.period)
statistic = "Average"
alarm_actions = lookup(var.sns_topic_arns_map, "redis_cpu_engine_utilization", var.default_sns_topic_arns)
ok_actions = lookup(var.sns_topic_arns_map, "redis_cpu_engine_utilization", var.default_sns_topic_arns)
threshold = "25"
threshold = tostring(var.alert_thresholds.redis_cpu_engine.threshold)
dimensions = {
CacheClusterId = each.key
@@ -282,15 +282,15 @@ resource "aws_cloudwatch_metric_alarm" "redis-database-memory-percentage" {
alarm_name = "redis-database-memory-percentage-${each.key}-${var.customer_prefix}"
alarm_description = "Percentage of the memory available for the cluster that is in use. This is calculated using used_memory/maxmemory."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
evaluation_periods = tostring(var.alert_thresholds.redis_memory.evaluation_periods)
metric_name = "DatabaseMemoryUsagePercentage"
namespace = "AWS/ElastiCache"
period = "300"
period = tostring(var.alert_thresholds.redis_memory.period)
statistic = "Average"
alarm_actions = lookup(var.sns_topic_arns_map, "redis_database_memory_percentage", var.default_sns_topic_arns)
ok_actions = lookup(var.sns_topic_arns_map, "redis_database_memory_percentage", var.default_sns_topic_arns)
threshold = "80"
threshold = tostring(var.alert_thresholds.redis_memory.threshold)
dimensions = {
CacheClusterId = each.key
@@ -372,9 +372,9 @@ resource "aws_cloudwatch_metric_alarm" "acm_certificate_expired" {
count = var.acm_certificate_arn == null ? 0 : 1
alarm_name = "acm-cert-expiry-${var.customer_prefix}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
period = "86400" // 1 day in seconds
threshold = 30 // days
evaluation_periods = tostring(var.alert_thresholds.acm_cert_expiry.evaluation_periods)
period = tostring(var.alert_thresholds.acm_cert_expiry.period)
threshold = var.alert_thresholds.acm_cert_expiry.threshold
statistic = "Average"
namespace = "AWS/CertificateManager"
metric_name = "DaysToExpiry"
+92
View File
@@ -80,6 +80,98 @@ variable "log_monitoring" {
default = {}
}
variable "alert_thresholds" {
description = "CloudWatch alarm threshold overrides. Each alarm type is optional; omitted fields use the defaults below."
type = object({
rds_cpu = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 80
period = 300
evaluation_periods = 1
})
redis_cpu = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 70
period = 300
evaluation_periods = 1
})
redis_cpu_engine = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 25
period = 300
evaluation_periods = 1
})
redis_memory = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 80
period = 300
evaluation_periods = 1
})
acm_cert_expiry = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 30
period = 86400
evaluation_periods = 1
})
alb_healthyhosts = optional(object({
threshold = number
period = number
evaluation_periods = number
}), {
threshold = 1
period = 60
evaluation_periods = 1
})
})
default = {
rds_cpu = {
threshold = 80
period = 300
evaluation_periods = 1
}
redis_cpu = {
threshold = 70
period = 300
evaluation_periods = 1
}
redis_cpu_engine = {
threshold = 25
period = 300
evaluation_periods = 1
}
redis_memory = {
threshold = 80
period = 300
evaluation_periods = 1
}
acm_cert_expiry = {
threshold = 30
period = 86400
evaluation_periods = 1
}
alb_healthyhosts = {
threshold = 1
period = 60
evaluation_periods = 1
}
}
}
variable "cron_monitoring" {
type = object({
mysql_host = string