Skip to content

Commit 97208e1

Browse files
committed
add ec2 alarms
1 parent 29230f9 commit 97208e1

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ override.tf.json
2727
# Ignore CLI configuration files
2828
.terraformrc
2929
terraform.rc
30+
31+
.idea/
32+
.vscode/

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ No modules.
243243
| <a name="input_placement_group"></a> [placement\_group](#input\_placement\_group) | The Placement Group to start the instance in | `string` | `null` | no |
244244
| <a name="input_private_dns_name_options"></a> [private\_dns\_name\_options](#input\_private\_dns\_name\_options) | Customize the private DNS name options of the instance | `map(string)` | `{}` | no |
245245
| <a name="input_private_ip"></a> [private\_ip](#input\_private\_ip) | Private IP address to associate with the instance in a VPC | `string` | `null` | no |
246-
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
247246
| <a name="input_root_block_device"></a> [root\_block\_device](#input\_root\_block\_device) | Customize details about the root block device of the instance. See Block Devices below for details | `list(any)` | `[]` | no |
248247
| <a name="input_secondary_private_ips"></a> [secondary\_private\_ips](#input\_secondary\_private\_ips) | A list of secondary private IPv4 addresses to assign to the instance's primary network interface (eth0) in a VPC. Can only be assigned to the primary network interface (eth0) attached at instance creation, not a pre-existing network interface i.e. referenced in a `network_interface block` | `list(string)` | `null` | no |
249248
| <a name="input_source_dest_check"></a> [source\_dest\_check](#input\_source\_dest\_check) | Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs | `bool` | `null` | no |

custom.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
data "aws_partition" "current" {}
22

33
locals {
4-
create = var.create && var.putin_khuylo
4+
create = var.create
55

66
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false
77

@@ -188,7 +188,13 @@ resource "aws_instance" "this" {
188188
delete = try(var.timeouts.delete, null)
189189
}
190190

191-
tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
191+
tags = merge({
192+
"Name" = var.name,
193+
"Environment" = var.environment,
194+
"Service" = var.service,
195+
"ServiceComponent" = var.service_component,
196+
"OwnerTeam" = var.owner_team
197+
}, var.instance_tags, var.tags)
192198
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
193199
}
194200

variables.tf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ variable "disable_api_stop" {
351351
default = null
352352

353353
}
354-
variable "putin_khuylo" {
355-
description = "Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo!"
356-
type = bool
357-
default = true
358-
}
354+
359355

360356
################################################################################
361357
# IAM Role / Instance Profile

wrappers/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ module "wrapper" {
5656
placement_group = try(each.value.placement_group, var.defaults.placement_group, null)
5757
private_dns_name_options = try(each.value.private_dns_name_options, var.defaults.private_dns_name_options, {})
5858
private_ip = try(each.value.private_ip, var.defaults.private_ip, null)
59-
putin_khuylo = try(each.value.putin_khuylo, var.defaults.putin_khuylo, true)
6059
root_block_device = try(each.value.root_block_device, var.defaults.root_block_device, [])
6160
secondary_private_ips = try(each.value.secondary_private_ips, var.defaults.secondary_private_ips, null)
6261
source_dest_check = try(each.value.source_dest_check, var.defaults.source_dest_check, null)

0 commit comments

Comments
 (0)