-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterruption.tf
98 lines (83 loc) · 2.64 KB
/
interruption.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
locals {
aws_partition_dns_suffix = data.aws_partition.current[0].dns_suffix
}
data "aws_partition" "current" {
count = var.enabled ? 1 : 0
}
resource "aws_sqs_queue" "this" {
count = var.enabled ? 1 : 0
#checkov:skip=CKV_AWS_27:Nothing sensitive
name = "${var.queue_interruption_prefix}-${var.helm_chart_name}"
message_retention_seconds = 300
tags = var.irsa_tags
}
data "aws_iam_policy_document" "queue" {
count = var.enabled ? 1 : 0
statement {
sid = "SqsWrite"
actions = ["sqs:SendMessage"]
resources = [aws_sqs_queue.this[0].arn]
principals {
type = "Service"
identifiers = [
"events.${local.aws_partition_dns_suffix}",
"sqs.${local.aws_partition_dns_suffix}",
]
}
}
}
resource "aws_sqs_queue_policy" "this" {
count = var.enabled ? 1 : 0
queue_url = aws_sqs_queue.this[0].url
policy = data.aws_iam_policy_document.queue[0].json
}
# Node Termination Event Rules
locals {
events = {
health_event = {
name = "HealthEvent"
description = "Karpenter interrupt - AWS health event"
event_pattern = {
source = ["aws.health"]
detail-type = ["AWS Health Event"]
}
}
spot_interupt = {
name = "SpotInterrupt"
description = "Karpenter interrupt - EC2 spot instance interruption warning"
event_pattern = {
source = ["aws.ec2"]
detail-type = ["EC2 Spot Instance Interruption Warning"]
}
}
instance_rebalance = {
name = "InstanceRebalance"
description = "Karpenter interrupt - EC2 instance rebalance recommendation"
event_pattern = {
source = ["aws.ec2"]
detail-type = ["EC2 Instance Rebalance Recommendation"]
}
}
instance_state_change = {
name = "InstanceStateChange"
description = "Karpenter interrupt - EC2 instance state-change notification"
event_pattern = {
source = ["aws.ec2"]
detail-type = ["EC2 Instance State-change Notification"]
}
}
}
}
resource "aws_cloudwatch_event_rule" "this" {
for_each = { for k, v in local.events : k => v if var.enabled }
name_prefix = "${var.rule_interruption_prefix}${each.value.name}-"
description = each.value.description
event_pattern = jsonencode(each.value.event_pattern)
tags = var.irsa_tags
}
resource "aws_cloudwatch_event_target" "this" {
for_each = { for k, v in local.events : k => v if var.enabled }
rule = aws_cloudwatch_event_rule.this[each.key].name
target_id = "KarpenterInterruptionQueueTarget"
arn = aws_sqs_queue.this[0].arn
}