-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhelm_node_termination_handler.tf
203 lines (171 loc) · 5.68 KB
/
helm_node_termination_handler.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
resource "helm_release" "node_termination_handler" {
count = var.node_termination_handler_toggle ? 1 : 0
name = "aws-node-termination-handler"
namespace = "kube-system"
chart = "aws-node-termination-handler"
repository = "https://aws.github.io/eks-charts/"
version = "0.21.0"
set {
name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = aws_iam_role.aws_node_termination_handler_role[count.index].arn
}
set {
name = "awsRegion"
value = var.aws_region
}
set {
name = "queueURL"
value = aws_sqs_queue.node_termination_handler[count.index].url
}
set {
name = "enableSqsTerminationDraining"
value = true
}
set {
name = "enableSpotInterruptionDraining"
value = true
}
set {
name = "enableRebalanceMonitoring"
value = true
}
set {
name = "enableRebalanceDraining"
value = true
}
set {
name = "enableScheduledEventDraining"
value = true
}
set {
name = "deleteSqsMsgIfNodeNotFound"
value = true
}
set {
name = "checkTagBeforeDraining"
value = false
}
}
resource "aws_sqs_queue" "node_termination_handler" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-aws-node-termination-handler", var.cluster_name)
delay_seconds = 0
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
visibility_timeout_seconds = 60
}
resource "aws_sqs_queue_policy" "node_termination_handler" {
count = var.node_termination_handler_toggle ? 1 : 0
queue_url = aws_sqs_queue.node_termination_handler[count.index].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"sqs:SendMessage"
],
"Resource": [
"${aws_sqs_queue.node_termination_handler[count.index].arn}"
]
}
]
}
EOF
}
resource "aws_cloudwatch_event_rule" "node_termination_handler_instance_terminate" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-node-termination-handler-instance-terminate", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.autoscaling"]
detail-type = [
"EC2 Instance-terminate Lifecycle Action"
]
})
}
resource "aws_cloudwatch_event_target" "node_termination_handler_instance_terminate" {
count = var.node_termination_handler_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.node_termination_handler_instance_terminate[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.node_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "node_termination_handler_scheduled_change" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-node-termination-handler-scheduled-change", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.health"]
detail-type = [
"AWS Health Event"
]
detail = {
service = [
"EC2"
]
eventTypeCategory = [
"scheduledChange"
]
}
})
}
resource "aws_cloudwatch_event_target" "node_termination_handler_scheduled_change" {
count = var.node_termination_handler_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.node_termination_handler_scheduled_change[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.node_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "node_termination_handler_spot_termination" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-node-termination-handler-spot-termination", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Spot Instance Interruption Warning"
]
})
}
resource "aws_cloudwatch_event_target" "node_termination_handler_spot_termination" {
count = var.node_termination_handler_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.node_termination_handler_spot_termination[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.node_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "node_termination_handler_rebalance" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-node-termination-handler-rebalance", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Instance Rebalance Recommendation"
]
})
}
resource "aws_cloudwatch_event_target" "node_termination_handler_rebalance" {
count = var.node_termination_handler_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.node_termination_handler_rebalance[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.node_termination_handler[count.index].arn
}
resource "aws_cloudwatch_event_rule" "node_termination_handler_state_change" {
count = var.node_termination_handler_toggle ? 1 : 0
name = format("%s-node-termination-handler-state-change", var.cluster_name)
description = var.cluster_name
event_pattern = jsonencode({
source = ["aws.ec2"]
detail-type = [
"EC2 Instance State-change Notification"
]
})
}
resource "aws_cloudwatch_event_target" "node_termination_handler_state_change" {
count = var.node_termination_handler_toggle ? 1 : 0
rule = aws_cloudwatch_event_rule.node_termination_handler_state_change[count.index].name
target_id = "SendToSQS"
arn = aws_sqs_queue.node_termination_handler[count.index].arn
}