This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_ecs_asg_instance_draining.tf
184 lines (157 loc) · 5.65 KB
/
lambda_ecs_asg_instance_draining.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
/*
This is a recreation of this cloudformation template: https://github.com/aws-samples/ecs-cid-sample/blob/master/cform/ecs.yaml
Reference this blog post for more information: https://aws.amazon.com/blogs/compute/how-to-automate-container-instance-draining-in-amazon-ecs/
*/
// Render the file and output the rendered version to the filesystem
// so that we can consume it with archive_file
data "archive_file" "ecs_asg_instance_drainer" {
type = "zip"
source_content = templatefile(
"${path.module}/lambda_functions/drainer/ecs_asg_instance_draining.py.tpl",
{
ecs_cluster_name = "${var.environment}-cluster",
aws_region = data.aws_region.current.name
}
)
source_content_filename = "ecs_asg_instance_draining.py"
output_path = "${path.module}/lambda_functions/drainer/ecs_asg_instance_draining.zip"
}
// Lambda
// Lambda IAM
data "aws_iam_policy_document" "ecs_asg_instance_drainer_role_policy" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_asg_instance_drainer_role" {
name = "${var.environment}-ecs-asg-instance-drainer"
assume_role_policy = data.aws_iam_policy_document.ecs_asg_instance_drainer_role_policy.json
tags = {
owner = "engineering"
Project = "infra"
service = "ecs-asg-instance-drainer"
env = var.environment
}
}
data "aws_iam_policy_document" "ecs_asg_instance_drainer_policy" {
statement {
effect = "Allow"
actions = [
"ecs:UpdateContainerInstancesState",
"ecs:DescribeContainerInstances"
]
resources = [
"arn:aws:ecs:*:${data.aws_caller_identity.current.account_id}:container-instance/*"
]
}
statement {
effect = "Allow"
actions = [
"ecs:ListContainerInstances"
]
resources = [
"arn:aws:ecs:*:${data.aws_caller_identity.current.account_id}:cluster/*"
]
}
statement {
effect = "Allow"
actions = [
"autoscaling:CompleteLifecycleAction"
]
resources = [
"arn:aws:autoscaling:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:autoScalingGroup:*:autoScalingGroupName/${var.environment}-ecs-*"
]
}
}
resource "aws_iam_policy" "ecs_asg_instance_drainer_policy" {
name = "${var.environment}-ecs-asg-instance-drainer"
path = "/"
policy = data.aws_iam_policy_document.ecs_asg_instance_drainer_policy.json
}
resource "aws_iam_role_policy_attachment" "ecs_asg_instance_drainer_attachment" {
role = aws_iam_role.ecs_asg_instance_drainer_role.name
policy_arn = aws_iam_policy.ecs_asg_instance_drainer_policy.arn
}
resource "aws_iam_role_policy_attachment" "ecs_asg_instance_drainer_logs_attachment" {
role = aws_iam_role.ecs_asg_instance_drainer_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
// Lambda function
resource "aws_lambda_function" "ecs_asg_instance_drainer" {
filename = data.archive_file.ecs_asg_instance_drainer.output_path
function_name = "${var.environment}-ecs-asg-instance-drainer"
description = "Drain ECS instances when their ASG terminates them"
role = aws_iam_role.ecs_asg_instance_drainer_role.arn
handler = "ecs_asg_instance_draining.lambda_handler"
runtime = "python3.6"
timeout = 60 * 6
memory_size = 128 * 2
source_code_hash = data.archive_file.ecs_asg_instance_drainer.output_base64sha256
tags = {
Project = "infra"
service = "ecs"
env = var.environment
}
depends_on = [
data.archive_file.ecs_asg_instance_drainer
]
}
// SNS topic
resource "aws_sns_topic" "ecs_asg_instance_drainer" {
name = "${var.environment}-ecs-asg-instance-drainer"
display_name = "${var.environment}-ecs-asg-instance-drainer"
tags = {
Project = "infra"
service = "ecs"
env = var.environment
}
}
// Log group
resource "aws_cloudwatch_log_group" "ecs_asg_instance_drainer" {
name = "/aws/lambda/${aws_lambda_function.ecs_asg_instance_drainer.function_name}"
retention_in_days = 3
}
// SNS IAM
resource "aws_lambda_permission" "ecs_asg_instance_drainer" {
statement_id = "AllowInvocationFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ecs_asg_instance_drainer.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.ecs_asg_instance_drainer.arn
}
// Subscribe lambda function to topic
resource "aws_sns_topic_subscription" "ecs_asg_instance_drainer" {
topic_arn = aws_sns_topic.ecs_asg_instance_drainer.arn
protocol = "lambda"
endpoint = aws_lambda_function.ecs_asg_instance_drainer.arn
}
// Alarm
resource "aws_cloudwatch_metric_alarm" "ecs_asg_instance_drainer_failure" {
alarm_name = "${var.environment}-ecs-asg-instance-drainer"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "Errors"
namespace = "AWS/Lambda"
period = "300"
statistic = "Maximum"
threshold = "0"
alarm_description = "Monitor ASG instance drainer for failures"
actions_enabled = true
ok_actions = []
alarm_actions = [] // You should actually send the alert somewhere in real life
insufficient_data_actions = []
treat_missing_data = "notBreaching"
dimensions = {
FunctionName = aws_lambda_function.ecs_asg_instance_drainer.function_name
}
tags = {
Project = "infra"
service = "ecs-asg-instance-drainer"
env = var.environment
}
}