|
| 1 | +variable "alarm_info_sns_topic_arn" { |
| 2 | + type = string |
| 3 | + description = "The ARN of the SNS topic to notify when on info alerts" |
| 4 | +} |
| 5 | + |
| 6 | +variable "alarm_sns_topic_arn" { |
| 7 | + type = string |
| 8 | + description = "The ARN of the SNS topic to notify when on critical alerts" |
| 9 | +} |
| 10 | + |
| 11 | +variable "environment" { |
| 12 | + type = string |
| 13 | + description = "The environment tag to apply to all resources. eg: production, testing, staging, etc" |
| 14 | + |
| 15 | + validation { |
| 16 | + condition = var.environment == null || can(regex("^(production|testing|staging|development)$", var.environment)) |
| 17 | + error_message = "environment must be lowercase alphanumeric with hyphens only." |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +variable "service" { |
| 22 | + type = string |
| 23 | + description = "Service hosted on this instance. eg: squadstack, metabase, grafana, etc" |
| 24 | + |
| 25 | + validation { |
| 26 | + condition = var.service == null || can(regex("^[a-z-]+$", var.service)) |
| 27 | + error_message = "service_component must be lowercase alphabets with hyphens only." |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +variable "service_component" { |
| 32 | + type = string |
| 33 | + default = null |
| 34 | + description = "[optional] Service Group within a service. eg: app, api, celery, etc" |
| 35 | + |
| 36 | + validation { |
| 37 | + condition = var.service_component == null || can(regex("^[a-z0-9-]+$", var.service_component)) |
| 38 | + error_message = "If provided, service_component must be lowercase alphanumeric with hyphens only." |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +variable "owner_team" { |
| 43 | + type = string |
| 44 | + description = "Owner of this ec2. eg: platform, supply, demand, ds, etc" |
| 45 | + |
| 46 | + validation { |
| 47 | + condition = var.owner_team == null || can(regex("^(platform|supply|demand|integrations|data-science)$", var.owner_team)) |
| 48 | + error_message = "owner_team must be amongst platform, supply, demand, integrations, data-science." |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +resource "aws_cloudwatch_metric_alarm" "ec2_cpuutilization_alert_info" { |
| 53 | + alarm_name = "${var.name}_high_cpu_alert" |
| 54 | + comparison_operator = "GreaterThanOrEqualToThreshold" |
| 55 | + evaluation_periods = "5" |
| 56 | + datapoints_to_alarm = "4" |
| 57 | + treat_missing_data = "missing" |
| 58 | + metric_name = "CPUUtilization" |
| 59 | + namespace = "AWS/EC2" |
| 60 | + period = "120" |
| 61 | + statistic = "Average" |
| 62 | + threshold = "85" |
| 63 | + alarm_description = "This metric monitors ec2 CPU Utilization" |
| 64 | + alarm_actions = [var.alarm_info_sns_topic_arn] |
| 65 | + unit = "Percent" |
| 66 | + dimensions = { |
| 67 | + InstanceId = try( |
| 68 | + aws_instance.this[0].id, |
| 69 | + aws_instance.ignore_ami[0].id, |
| 70 | + ) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +resource "aws_cloudwatch_metric_alarm" "ec2_cpuutilization_alert_warning" { |
| 77 | + alarm_name = "${var.name}_critical_cpu_alert" |
| 78 | + comparison_operator = "GreaterThanOrEqualToThreshold" |
| 79 | + evaluation_periods = "15" |
| 80 | + datapoints_to_alarm = "12" |
| 81 | + treat_missing_data = "breaching" |
| 82 | + metric_name = "CPUUtilization" |
| 83 | + namespace = "AWS/EC2" |
| 84 | + period = "120" |
| 85 | + statistic = "Maximum" |
| 86 | + threshold = "95" |
| 87 | + alarm_description = "This metric monitors ec2 CPU Utilization" |
| 88 | + alarm_actions = [var.alarm_sns_topic_arn] |
| 89 | + unit = "Percent" |
| 90 | + dimensions = { |
| 91 | + InstanceId = try( |
| 92 | + aws_instance.this[0].id, |
| 93 | + aws_instance.ignore_ami[0].id, |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments