-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
283 lines (259 loc) · 8.9 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
locals {
ecr_repo = "docker-runner-image" # ECR repo name
image_tag = "latest" # image tag
dkr_img_src_path = "${path.module}/docker-src"
dkr_img_src_sha256 = sha256(join("", [for f in fileset(".", "${local.dkr_img_src_path}/**") : file(f)]))
dkr_build_cmd = <<-EOT
docker build -t ${aws_ecr_repository.runner_image.repository_url}:${local.image_tag} ${local.dkr_img_src_path}
aws ecr get-login-password --region ${data.aws_region.current.name} | docker login --username AWS --password-stdin ${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.name}.amazonaws.com
docker push ${aws_ecr_repository.runner_image.repository_url}:${local.image_tag}
EOT
github_token_arn = var.secret_arn_override == null ? aws_secretsmanager_secret.github_token[0].arn : var.secret_arn_override
runners = { for name, runner_config in var.runners : name => merge(var.default_runner_config, runner_config) }
}
resource "aws_ecr_repository" "runner_image" {
name = local.ecr_repo
image_tag_mutability = "MUTABLE"
encryption_configuration {
encryption_type = "KMS"
}
image_scanning_configuration {
scan_on_push = true
}
tags = local.tags
}
resource "null_resource" "push_ecr" {
triggers = {
detect_docker_source_changes = var.force_image_rebuild == true ? timestamp() : local.dkr_img_src_sha256,
aws_ecr_repository = aws_ecr_repository.runner_image.repository_url
}
provisioner "local-exec" {
command = local.dkr_build_cmd
}
}
resource "aws_ecs_cluster" "github_runner_cluster" {
name = var.cluster_name
setting {
name = "containerInsights"
value = "enabled"
}
tags = local.tags
}
resource "aws_ecs_service" "runner" {
for_each = local.runners
name = each.key
cluster = aws_ecs_cluster.github_runner_cluster.id
task_definition = aws_ecs_task_definition.runner[each.key].arn
desired_count = each.value.scale_target_min_capacity
launch_type = "FARGATE"
force_new_deployment = var.force_image_rebuild
network_configuration {
subnets = var.subnet_ids
security_groups = var.security_group_ids
assign_public_ip = false
}
lifecycle {
ignore_changes = [desired_count]
}
tags = local.tags
}
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
for_each = local.runners
alarm_name = "${each.key}-cpu-high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = each.value.max_cpu_evaluation_period
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = each.value.max_cpu_period
statistic = "Maximum"
threshold = each.value.max_cpu_threshold
dimensions = {
ClusterName = aws_ecs_cluster.github_runner_cluster.name
ServiceName = aws_ecs_service.runner[each.key].name
}
alarm_actions = [aws_appautoscaling_policy.scale_up_policy[each.key].arn]
tags = var.tags
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
for_each = local.runners
alarm_name = "${each.key}-cpu-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = each.value.min_cpu_evaluation_period
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = each.value.min_cpu_period
statistic = "Average"
threshold = each.value.min_cpu_threshold
dimensions = {
ClusterName = aws_ecs_cluster.github_runner_cluster.name
ServiceName = aws_ecs_service.runner[each.key].name
}
alarm_actions = [aws_appautoscaling_policy.scale_down_policy[each.key].arn]
tags = var.tags
}
resource "aws_appautoscaling_policy" "scale_up_policy" {
for_each = local.runners
name = "${each.key}-scale-up-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.github_runner_cluster.name}/${aws_ecs_service.runner[each.key].name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
resource "aws_appautoscaling_policy" "scale_down_policy" {
for_each = local.runners
name = "${each.key}-scale-down-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.github_runner_cluster.name}/${aws_ecs_service.runner[each.key].name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
resource "aws_appautoscaling_target" "scale_target" {
for_each = local.runners
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.github_runner_cluster.name}/${aws_ecs_service.runner[each.key].name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = each.value.scale_target_min_capacity
max_capacity = each.value.scale_target_max_capacity
}
resource "aws_secretsmanager_secret" "github_token" {
count = var.secret_arn_override == null ? 1 : 0
name = var.secret_name
tags = var.tags
}
resource "aws_secretsmanager_secret_version" "github_token" {
count = var.secret_arn_override == null ? 1 : 0
secret_id = aws_secretsmanager_secret.github_token[0].id
secret_string = var.runner_token
}
resource "aws_iam_role" "ecs_task_execution_role" {
name = var.ecs_task_execution_role_name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})
inline_policy {
name = "aws-ecs-github-runners-role"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "SecretsManager"
Effect = "Allow"
Action = [
"secretsmanager:GetSecretValue"
]
Resource = "${regex("^arn:aws:secretsmanager:[a-z0-9-]+:[0-9]{12}:secret:[^:]+", local.github_token_arn)}"
},
{
Sid = "ECR"
Effect = "Allow"
Action = [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
"ecr:GetAuthorizationToken"
]
Resource = "*"
},
{
Sid = "CloudWatchLogs"
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:DeleteLogGroup",
"logs:CreateLogStream",
"logs:DeleteLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
]
Resource = "arn:aws:logs:*:*:*"
},
]
})
}
tags = var.tags
}
resource "aws_ecs_task_definition" "runner" {
for_each = local.runners
family = "${var.cluster_name}_${each.key}"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = 1024 * try(each.value.cpu, 1)
memory = 1024 * try(each.value.memory, 3)
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
"name" : "${each.key}",
"image" : "${aws_ecr_repository.runner_image.repository_url}:${local.image_tag}",
"portMappings" : [
{
"name" : "${lower(each.key)}-8080-tcp",
"containerPort" : 8080,
"hostPort" : 8080,
"protocol" : "tcp",
"appProtocol" : "http"
}
],
"environment" : [
{
"name" : "RUNNER_PREFIX",
"value" : "${each.value.runner_prefix}"
},
{
"name" : "GH_OWNER",
"value" : "${each.value.org}"
},
{
"name" : "LABELS",
"value" : "${each.value.labels}"
},
{
"name" : "RUNNER_GROUP",
"value" : "${each.value.runner_group == null ? "Default" : each.value.runner_group}"
}
]
"secrets" : [
{
"name" : "GH_TOKEN",
"valueFrom" : "${local.github_token_arn}"
}
],
"essential" : true,
"logConfiguration" : {
"logDriver" : "awslogs",
"options" : {
"awslogs-create-group" : "true",
"awslogs-group" : "/ecs/aws-ecs-github-runners",
"awslogs-region" : "${data.aws_region.current.name}",
"awslogs-stream-prefix" : "ecs"
}
}
}
])
tags = var.tags
}