-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
338 lines (287 loc) · 8.48 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
locals {
iam_policy_statements_cloudwatch = (
length(var.cloudwatch_log_group) > 0
? [{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
]
Resource = "*"
}]
: [])
}
resource "aws_iam_policy" "this" {
count = var.create_iam_resources ? 1 : 0
name = var.naming_prefix
policy = jsonencode({
Version = "2012-10-17"
Statement = concat([
{
Action = ["ssm:GetParameter*"]
Effect = "Allow"
Resource = data.aws_ssm_parameter.this.arn
},
{
Action = ["ec2:CreateTags"]
Effect = "Allow"
Resource = "arn:aws:ec2:${var.region}:${data.aws_caller_identity.current.account_id}:*/*"
Condition = {
StringEquals = {
"aws:ResourceTag/Name" = var.naming_prefix
"ec2:CreateAction" = "RunInstances"
}
}
}
],
local.iam_policy_statements_cloudwatch)
})
}
resource "aws_iam_role" "this" {
count = var.create_iam_resources ? 1 : 0
name = var.naming_prefix
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
tags = {
Name = var.naming_prefix
}
}
resource "aws_iam_role_policy_attachment" "this" {
count = var.create_iam_resources ? 1 : 0
role = var.naming_prefix
policy_arn = aws_iam_policy.this[count.index].arn
}
resource "aws_iam_role_policy_attachment" "user_defined_policies" {
count = length(var.iam_policy_arns)
role = var.naming_prefix
policy_arn = var.iam_policy_arns[count.index]
}
resource "aws_iam_instance_profile" "this" {
count = var.create_iam_resources ? 1 : 0
name = var.naming_prefix
role = var.naming_prefix
}
locals {
iam_instance_profile_arn = (
length(var.iam_instance_profile_arn) == 0
? aws_iam_instance_profile.this[0].arn
: var.iam_instance_profile_arn
)
}
resource "aws_security_group" "this" {
count = length(var.security_groups) > 0 ? 0 : 1
name = var.naming_prefix
description = "GitHub runner instance ${var.naming_prefix}"
# tfsec:ignore:aws-ec2-no-public-egress-sgr
egress {
description = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
vpc_id = var.vpc_id
}
locals {
security_groups = (
length(var.security_groups) > 0
? var.security_groups
: flatten(aws_security_group.this[*].id)
)
default_per_instance_runner_count = (
data.aws_ec2_instance_type.this.default_vcpus
* data.aws_ec2_instance_type.this.default_cores
* data.aws_ec2_instance_type.this.default_threads_per_core
)
per_instance_runner_count = (
var.per_instance_runner_count == -1
? local.default_per_instance_runner_count
: var.per_instance_runner_count
)
}
module "software_packs" {
source = "./modules/software"
count = length(var.software_packs)
software_pack = var.software_packs[count.index]
}
module "user_data" {
source = "./modules/user_data"
config = {
region = var.region
cloudwatch_log_group = var.cloudwatch_log_group
github_url = var.github_url
github_organisation_name = var.github_organisation_name
cloud_init_packages = distinct(
concat(
flatten(module.software_packs[*].packages),
var.cloud_init_extra_packages
)
)
cloud_init_runcmds = concat(
flatten(module.software_packs[*].runcmds),
var.cloud_init_extra_runcmds
)
cloud_init_write_files = var.cloud_init_extra_write_files
cloud_init_other = var.cloud_init_extra_other
per_instance_runner_count = local.per_instance_runner_count
runner_group = var.github_runner_group
runner_labels = var.github_runner_labels
ssm_parameter_name = var.ssm_parameter_name
}
}
resource "aws_launch_template" "this" {
name = var.naming_prefix
block_device_mappings {
device_name = "/dev/sda1"
ebs {
encrypted = true
delete_on_termination = true
volume_size = (
var.ec2_ebs_volume_size == -1
? local.per_instance_runner_count * 20
: var.ec2_ebs_volume_size
)
}
}
iam_instance_profile {
arn = local.iam_instance_profile_arn
}
image_id = data.aws_ami.ami.id
instance_market_options {
market_type = "spot"
}
instance_type = var.ec2_instance_type
key_name = var.ec2_key_pair_name
network_interfaces {
associate_public_ip_address = var.ec2_associate_public_ip_address
security_groups = local.security_groups
}
user_data = base64gzip(module.user_data.user_data)
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
lifecycle {
create_before_destroy = true
}
}
locals {
autoscaling_group_name = var.naming_prefix
}
resource "aws_autoscaling_group" "this" {
count = (var.scaling_mode == "autoscaling-group" ? 1 : 0)
name = local.autoscaling_group_name
min_size = var.autoscaling_min_size
max_size = var.autoscaling_max_size
desired_capacity = var.autoscaling_desired_size
launch_template {
id = aws_launch_template.this.id
# trigger instance refresh
version = aws_launch_template.this.latest_version
}
max_instance_lifetime = var.autoscaling_max_instance_lifetime
instance_refresh {
strategy = "Rolling"
}
vpc_zone_identifier = var.subnet_ids
lifecycle {
ignore_changes = [desired_capacity, target_group_arns]
}
tag {
key = "Name"
value = var.naming_prefix
propagate_at_launch = true
}
}
resource "aws_autoscaling_schedule" "on" {
count = (
var.scaling_mode == "autoscaling-group"
? length(var.autoscaling_schedule_on_recurrences)
: 0)
scheduled_action_name = "${var.naming_prefix}-on-${count.index}"
min_size = var.autoscaling_min_size
max_size = var.autoscaling_max_size
desired_capacity = var.autoscaling_desired_size
recurrence = var.autoscaling_schedule_on_recurrences[count.index]
time_zone = var.autoscaling_schedule_time_zone
autoscaling_group_name = local.autoscaling_group_name
depends_on = [
aws_autoscaling_group.this
]
}
resource "aws_autoscaling_schedule" "off" {
count = (
var.scaling_mode == "autoscaling-group"
? length(var.autoscaling_schedule_off_recurrences)
: 0)
scheduled_action_name = "${var.naming_prefix}-off-${count.index}"
min_size = 0
max_size = 0
desired_capacity = 0
recurrence = var.autoscaling_schedule_off_recurrences[count.index]
time_zone = var.autoscaling_schedule_time_zone
autoscaling_group_name = local.autoscaling_group_name
depends_on = [
aws_autoscaling_group.this
]
}
resource "aws_autoscaling_policy" "scale_down" {
count = (var.scaling_mode == "autoscaling-group" ? 1 : 0)
name = "${var.naming_prefix}-scale-down"
autoscaling_group_name = local.autoscaling_group_name
adjustment_type = "ChangeInCapacity"
scaling_adjustment = -1
cooldown = 120
depends_on = [
aws_autoscaling_group.this
]
}
resource "aws_cloudwatch_metric_alarm" "scale_down" {
count = (var.scaling_mode == "autoscaling-group" ? 1 : 0)
alarm_description = "Monitor CPU for ASG ${local.autoscaling_group_name}"
alarm_actions = [aws_autoscaling_policy.scale_down[count.index].arn]
alarm_name = "${var.naming_prefix}-scale-down"
comparison_operator = "LessThanOrEqualToThreshold"
namespace = "AWS/EC2"
metric_name = "CPUUtilization"
threshold = "10"
evaluation_periods = "2"
period = "120"
statistic = "Average"
dimensions = {
AutoScalingGroupName = local.autoscaling_group_name
}
depends_on = [
aws_autoscaling_group.this
]
}
resource "aws_instance" "this" {
count = var.scaling_mode == "single-instance" ? 1 : 0
launch_template {
id = aws_launch_template.this.id
version = aws_launch_template.this.latest_version
}
root_block_device {
encrypted = true
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
subnet_id = var.subnet_ids[0]
tags = {
Name = var.naming_prefix
}
}