Skip to content

Commit

Permalink
Add slack notification to cloudwatch alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Jan 11, 2024
1 parent ed9296c commit 6eeb15c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
60 changes: 60 additions & 0 deletions monitoring/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions monitoring/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module "core" {
}

locals {
alarm_actions = concat(var.alarm_actions, var.slack_webhook.url == "" ? [] : [module.notify_slack.slack_topic_arn])

tags = merge(
module.core.outputs.stack.tags,
{
Expand All @@ -42,7 +44,7 @@ resource "aws_cloudwatch_metric_alarm" "load_balancer_5xx" {
for_each = toset(var.load_balancers)

actions_enabled = var.actions_enabled
alarm_actions = var.alarm_actions
alarm_actions = local.alarm_actions
alarm_name = "${each.key}-LoadBalancer5XX"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 3
Expand All @@ -64,7 +66,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization" {
for_each = local.services

actions_enabled = var.actions_enabled
alarm_actions = var.alarm_actions
alarm_actions = local.alarm_actions
alarm_name = "${each.value.service}-CPUUtilization"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 3
Expand All @@ -86,7 +88,7 @@ resource "aws_cloudwatch_metric_alarm" "memory_utilization" {
for_each = local.services

actions_enabled = var.actions_enabled
alarm_actions = var.alarm_actions
alarm_actions = local.alarm_actions
alarm_name = "${each.value.service}-MemoryUtilization"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 3
Expand All @@ -102,4 +104,16 @@ resource "aws_cloudwatch_metric_alarm" "memory_utilization" {
}

tags = local.tags
}

data "aws_route53_zone" "status_zone" {
name = var.status_zone_name
}

resource "aws_route53_record" "honeybadger_status_page" {
zone_id = data.aws_route53_zone.status_zone.id
name = "status.${data.aws_route53_zone.status_zone.name}"
type = "CNAME"
records = ["status.hbuptime.com"]
ttl = 300
}
11 changes: 11 additions & 0 deletions monitoring/slack_notification.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "notify_slack" {
source = "terraform-aws-modules/notify-slack/aws"
version = "6.1.0"

create_sns_topic = true
sns_topic_name = "${module.core.outputs.stack.namespace}-slack-notification"
lambda_function_name = "${module.core.outputs.stack.namespace}-slack-notification"
slack_channel = var.slack_webhook.channel
slack_username = var.slack_webhook.username
slack_webhook_url = var.slack_webhook.url
}
13 changes: 13 additions & 0 deletions monitoring/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ variable "services" {
type = map(list(string))
default = {}
}

variable "slack_webhook" {
type = map(string)
default = {
channel = ""
url = ""
username = ""
}
}

variable "status_zone_name" {
type = string
}

0 comments on commit 6eeb15c

Please sign in to comment.