Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
feat: Cloudwatch alarm notify settings (dasmeta#254)
Browse files Browse the repository at this point in the history
* feat(cloudwatch-alarm-notify) configurable prefixes for alarm names

* feat(cloudwatch-alarm-notify) custom sns topic arn for alarms

* fix(cloudwatch-alarm-notify) replace % characters in tag names

Sometimes you want the metric name in the name of the alarm. Some of the
metrics contain a % sign which is not allowed in tag values.
  • Loading branch information
crigertg authored Nov 7, 2022
1 parent b480346 commit 3303285
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions modules/cloudwatch-alarm-notify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ No requirements.
| <a name="input_alarm_actions"></a> [alarm\_actions](#input\_alarm\_actions) | n/a | `list(string)` | `[]` | no |
| <a name="input_alarm_description"></a> [alarm\_description](#input\_alarm\_description) | n/a | `string` | `""` | no |
| <a name="input_alarm_name"></a> [alarm\_name](#input\_alarm\_name) | Domain name or ip address of checking service. | `string` | n/a | yes |
| <a name="input_alarm_prefix_down"></a> [alarm\_prefix\_down](#input\_alarm\_prefix\_down) | A prefix for the alarm message when the host is down. The default is a slack emoji. | `string` | `":x: "` | no |
| <a name="input_alarm_prefix_up"></a> [alarm\_prefix\_up](#input\_alarm\_prefix\_up) | A prefix for the alarm message when the host is up. The default is a slack emoji. | `string` | `":white_check_mark: "` | no |
| <a name="input_alert_type_name"></a> [alert\_type\_name](#input\_alert\_type\_name) | Alert\_Type | `string` | `"other"` | no |
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Specifies the number of days you want to retain log events in log group for Lambda. | `number` | `0` | no |
| <a name="input_comparison_operator"></a> [comparison\_operator](#input\_comparison\_operator) | Comparison operator. | `string` | `""` | no |
Expand All @@ -135,6 +137,7 @@ No requirements.
| <a name="input_sms_message_body"></a> [sms\_message\_body](#input\_sms\_message\_body) | n/a | `string` | `"sms_message_body"` | no |
| <a name="input_sns_subscription_email_address_list"></a> [sns\_subscription\_email\_address\_list](#input\_sns\_subscription\_email\_address\_list) | List of email addresses | `list(string)` | `[]` | no |
| <a name="input_sns_subscription_phone_number_list"></a> [sns\_subscription\_phone\_number\_list](#input\_sns\_subscription\_phone\_number\_list) | List of telephone numbers to subscribe to SNS. | `list(string)` | `[]` | no |
| <a name="input_sns_topic_arn"></a> [sns\_topic\_arn](#input\_sns\_topic\_arn) | The ARN of an SNS topic to which notifications will be sent. This does not relate to the other SNS topic variables. | `string` | `null` | no |
| <a name="input_statistic"></a> [statistic](#input\_statistic) | Statistic. | `string` | `""` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags object. | `map` | `{}` | no |
| <a name="input_threshold"></a> [threshold](#input\_threshold) | Threshold. | `string` | `""` | no |
Expand Down
18 changes: 11 additions & 7 deletions modules/cloudwatch-alarm-notify/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ locals {
alarm_description_down = "This metric monitors ${var.alarm_name} when threshold > ${var.threshold != "" ? var.threshold : lookup(local.default_alert_variables_object, "threshold", "default_threshold")}"
default_alert_variables_object = lookup(local.default, var.alert_type_name, {})
actions = concat(
aws_sns_topic.k8s-alerts-notify-email.*.arn, // email
aws_sns_topic.k8s-alerts-notify-sms.*.arn, // sms
aws_sns_topic.k8s-alerts-notify-opsgenie.*.arn, // Opsgenie
module.notify_slack.*.this_slack_topic_arn // slack
aws_sns_topic.k8s-alerts-notify-email.*.arn, // email
aws_sns_topic.k8s-alerts-notify-sms.*.arn, // sms
aws_sns_topic.k8s-alerts-notify-opsgenie.*.arn, // Opsgenie
module.notify_slack.*.this_slack_topic_arn, // slack
var.sns_topic_arn == null ? [] : [var.sns_topic_arn] // custom
)
# ensure that there is no % in the tag value. Sometimes you want the name of
# the metric to be part of the alarm name. Some metrics have the % sign in the name.
tag_name = replace("${var.alarm_name}-alerts", "%", "Percent")
}

### Create a cloudwatch healthcheck metric alarm
resource "aws_cloudwatch_metric_alarm" "metric-alarm-down" {
alarm_name = ":x: ${var.alarm_name}"
alarm_name = "${var.alarm_prefix_down}${var.alarm_name}"
namespace = var.namespace != "" ? var.namespace : lookup(local.default_alert_variables_object, "namespace", "default_namespace")
metric_name = var.metric_name != "" ? var.metric_name : lookup(local.default_alert_variables_object, "metric_name", "default_metric_name")
comparison_operator = var.comparison_operator != "" ? var.comparison_operator : lookup(local.default_alert_variables_object, "comparison_operator", "default_comparison_operator")
Expand All @@ -82,7 +86,7 @@ resource "aws_cloudwatch_metric_alarm" "metric-alarm-down" {


resource "aws_cloudwatch_metric_alarm" "metric-alarm-up" {
alarm_name = ":white_check_mark: ${var.alarm_name}"
alarm_name = "${var.alarm_prefix_down}${var.alarm_name}"
namespace = var.namespace != "" ? var.namespace : lookup(local.default_alert_variables_object, "namespace", "default_namespace")
metric_name = var.metric_name != "" ? var.metric_name : lookup(local.default_alert_variables_object, "metric_name", "default_metric_name")
comparison_operator = var.comparison_operator != "" ? var.comparison_operator : lookup(local.default_alert_variables_object, "comparison_operator", "default_comparison_operator")
Expand All @@ -98,6 +102,6 @@ resource "aws_cloudwatch_metric_alarm" "metric-alarm-up" {
ok_actions = local.actions

tags = {
Name = "${var.alarm_name}-alerts"
Name = local.tag_name
}
}
18 changes: 18 additions & 0 deletions modules/cloudwatch-alarm-notify/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ variable "alarm_name" {
description = "Domain name or ip address of checking service."
}

variable "alarm_prefix_down" {
description = "A prefix for the alarm message when the host is down. The default is a slack emoji."
type = string
default = ":x: "
}

variable "alarm_prefix_up" {
description = "A prefix for the alarm message when the host is up. The default is a slack emoji."
type = string
default = ":white_check_mark: "
}

variable "tags" {
# type = object
default = {}
Expand Down Expand Up @@ -86,6 +98,12 @@ variable "alarm_description" {
}

### SNS Topic related variables
variable "sns_topic_arn" {
type = string
description = "The ARN of an SNS topic to which notifications will be sent. This does not relate to the other SNS topic variables."
default = null
}

variable "sns_subscription_email_address_list" {
type = list(string)
default = []
Expand Down

0 comments on commit 3303285

Please sign in to comment.