forked from cattle-ops/terraform-aws-gitlab-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
396 lines (328 loc) · 13.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
resource "aws_key_pair" "key" {
count = var.ssh_key_pair == "" ? 1 : 0
key_name = "${var.environment}-gitlab-runner"
public_key = var.ssh_public_key
}
resource "aws_security_group" "runner" {
name_prefix = "${var.environment}-security-group"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
local.tags,
{
"Name" = format("%s", local.name_sg)
},
)
}
resource "aws_security_group_rule" "runner_ssh" {
count = var.enable_gitlab_runner_ssh_access ? 1 : 0
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.gitlab_runner_ssh_cidr_blocks
security_group_id = aws_security_group.runner.id
}
resource "aws_security_group" "docker_machine" {
name_prefix = "${var.environment}-docker-machine"
vpc_id = var.vpc_id
tags = merge(
local.tags,
{
"Name" = format("%s", local.name_sg)
},
)
}
resource "aws_security_group_rule" "docker_machine_docker" {
type = "ingress"
from_port = 2376
to_port = 2376
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.docker_machine.id
}
resource "aws_security_group_rule" "docker_machine_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.docker_machine_ssh_cidr_blocks
security_group_id = aws_security_group.docker_machine.id
}
resource "aws_security_group_rule" "out_all" {
type = "egress"
from_port = 0
to_port = 65535
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.docker_machine.id
}
# Parameter value is managed by the user-data script of the gitlab runner instance
resource "aws_ssm_parameter" "runner_registration_token" {
name = local.secure_parameter_store_runner_token_key
type = "SecureString"
value = "null"
tags = local.tags
lifecycle {
ignore_changes = [value]
}
}
data "template_file" "user_data" {
template = file("${path.module}/template/user-data.tpl")
vars = {
logging = var.enable_cloudwatch_logging ? data.template_file.logging.rendered : ""
gitlab_runner = data.template_file.gitlab_runner.rendered
user_data_trace_log = var.enable_runner_user_data_trace_log
}
}
data "template_file" "logging" {
template = file("${path.module}/template/logging.tpl")
vars = {
environment = var.environment
}
}
data "template_file" "gitlab_runner" {
template = file("${path.module}/template/gitlab-runner.tpl")
vars = {
gitlab_runner_version = var.gitlab_runner_version
docker_machine_version = var.docker_machine_version
runners_config = data.template_file.runners.rendered
runners_executor = var.runners_executor
pre_install = var.userdata_pre_install
post_install = var.userdata_post_install
runners_gitlab_url = var.runners_gitlab_url
runners_token = var.runners_token
secure_parameter_store_runner_token_key = local.secure_parameter_store_runner_token_key
secure_parameter_store_region = var.aws_region
gitlab_runner_registration_token = var.gitlab_runner_registration_config["registration_token"]
giltab_runner_description = var.gitlab_runner_registration_config["description"]
gitlab_runner_tag_list = var.gitlab_runner_registration_config["tag_list"]
gitlab_runner_locked_to_project = var.gitlab_runner_registration_config["locked_to_project"]
gitlab_runner_run_untagged = var.gitlab_runner_registration_config["run_untagged"]
gitlab_runner_maximum_timeout = var.gitlab_runner_registration_config["maximum_timeout"]
}
}
data "template_file" "runners" {
template = file("${path.module}/template/runner-config.tpl")
vars = {
aws_region = var.aws_region
gitlab_url = var.runners_gitlab_url
runners_vpc_id = var.vpc_id
runners_subnet_id = var.subnet_id_runners
runners_aws_zone = var.aws_zone
runners_instance_type = var.docker_machine_instance_type
runners_spot_price_bid = var.docker_machine_spot_price_bid
runners_ami = data.aws_ami.docker-machine.id
runners_security_group_name = aws_security_group.docker_machine.name
runners_monitoring = var.runners_monitoring
runners_instance_profile = aws_iam_instance_profile.docker_machine.name
runners_additional_volumes = local.runners_additional_volumes
docker_machine_options = length(var.docker_machine_options) == 0 ? "" : local.docker_machine_options_string
runners_name = var.runners_name
runners_tags = var.overrides["name_docker_machine_runners"] == "" ? format(
"%s,Name,%s-docker-machine",
local.tags_string,
var.environment,
) : format(
"%s,Name,%s",
local.tags_string,
var.overrides["name_docker_machine_runners"],
)
runners_token = var.runners_token
runners_executor = var.runners_executor
runners_limit = var.runners_limit
runners_concurrent = var.runners_concurrent
runners_image = var.runners_image
runners_privileged = var.runners_privileged
runners_shm_size = var.runners_shm_size
runners_pull_policy = var.runners_pull_policy
runners_idle_count = var.runners_idle_count
runners_idle_time = var.runners_idle_time
runners_off_peak_timezone = var.runners_off_peak_timezone
runners_off_peak_idle_count = var.runners_off_peak_idle_count
runners_off_peak_idle_time = var.runners_off_peak_idle_time
runners_off_peak_periods_string = local.runners_off_peak_periods_string
runners_root_size = var.runners_root_size
runners_iam_instance_profile_name = var.runners_iam_instance_profile_name
runners_use_private_address = var.runners_use_private_address
runners_environment_vars = jsonencode(var.runners_environment_vars)
runners_pre_build_script = var.runners_pre_build_script
runners_post_build_script = var.runners_post_build_script
runners_pre_clone_script = var.runners_pre_clone_script
runners_request_concurrency = var.runners_request_concurrency
runners_output_limit = var.runners_output_limit
bucket_name = local.bucket_name
shared_cache = var.cache_shared
}
}
data "aws_ami" "docker-machine" {
most_recent = "true"
dynamic "filter" {
for_each = var.runner_ami_filter
content {
name = filter.key
values = filter.value
}
}
owners = var.runner_ami_owners
}
resource "aws_autoscaling_group" "gitlab_runner_instance" {
name = "${var.environment}-as-group"
vpc_zone_identifier = var.subnet_ids_gitlab_runner
min_size = "1"
max_size = "1"
desired_capacity = "1"
health_check_grace_period = 0
launch_configuration = aws_launch_configuration.gitlab_runner_instance.name
tags = concat(
data.null_data_source.tags.*.outputs,
[
{
"key" = "Name"
"value" = local.name_runner_instance
"propagate_at_launch" = true
},
],
)
}
data "aws_ami" "runner" {
most_recent = "true"
dynamic "filter" {
for_each = var.ami_filter
content {
name = filter.key
values = filter.value
}
}
owners = var.ami_owners
}
resource "aws_launch_configuration" "gitlab_runner_instance" {
security_groups = [aws_security_group.runner.id]
key_name = var.ssh_key_pair == "" ? aws_key_pair.key[0].key_name : var.ssh_key_pair
image_id = data.aws_ami.runner.id
user_data = data.template_file.user_data.rendered
instance_type = var.instance_type
spot_price = var.runner_instance_spot_price
iam_instance_profile = aws_iam_instance_profile.instance.name
associate_public_ip_address = false == var.runners_use_private_address
lifecycle {
create_before_destroy = true
}
}
################################################################################
### Create cache bucket
################################################################################
locals {
bucket_name = var.cache_bucket["create"] ? module.cache.bucket : var.cache_bucket["bucket"]
bucket_policy = var.cache_bucket["create"] ? module.cache.policy_arn : var.cache_bucket["policy"]
}
module "cache" {
source = "./cache"
environment = var.environment
tags = local.tags
create_cache_bucket = var.cache_bucket["create"]
cache_bucket_prefix = var.cache_bucket_prefix
cache_bucket_name_include_account_id = var.cache_bucket_name_include_account_id
cache_bucket_versioning = var.cache_bucket_versioning
cache_expiration_days = var.cache_expiration_days
}
################################################################################
### Trust policy
################################################################################
resource "aws_iam_instance_profile" "instance" {
name = "${var.environment}-instance-profile"
role = aws_iam_role.instance.name
}
data "template_file" "instance_role_trust_policy" {
template = length(var.instance_role_json) > 0 ? var.instance_role_json : file("${path.module}/policies/instance-role-trust-policy.json")
}
resource "aws_iam_role" "instance" {
name = "${var.environment}-instance-role"
assume_role_policy = data.template_file.instance_role_trust_policy.rendered
}
################################################################################
### Policies for runner agent instance to create docker machines via spot req.
################################################################################
data "template_file" "instance_docker_machine_policy" {
template = file(
"${path.module}/policies/instance-docker-machine-policy.json",
)
}
resource "aws_iam_policy" "instance_docker_machine_policy" {
name = "${var.environment}-docker-machine"
path = "/"
description = "Policy for docker machine."
policy = data.template_file.instance_docker_machine_policy.rendered
}
resource "aws_iam_role_policy_attachment" "instance_docker_machine_policy" {
role = aws_iam_role.instance.name
policy_arn = aws_iam_policy.instance_docker_machine_policy.arn
}
################################################################################
### Policy for the docker machine instance to access cache
################################################################################
resource "aws_iam_role_policy_attachment" "docker_machine_cache_instance" {
role = aws_iam_role.instance.name
policy_arn = local.bucket_policy
}
################################################################################
### docker machine instance policy
################################################################################
data "template_file" "dockermachine_role_trust_policy" {
template = length(var.docker_machine_role_json) > 0 ? var.docker_machine_role_json : file("${path.module}/policies/instance-role-trust-policy.json")
}
resource "aws_iam_role" "docker_machine" {
name = "${var.environment}-docker-machine-role"
assume_role_policy = data.template_file.dockermachine_role_trust_policy.rendered
}
resource "aws_iam_instance_profile" "docker_machine" {
name = "${var.environment}-docker-machine-profile"
role = aws_iam_role.docker_machine.name
}
################################################################################
### Service linked policy, optional
################################################################################
data "template_file" "service_linked_role" {
count = var.allow_iam_service_linked_role_creation ? 1 : 0
template = file(
"${path.module}/policies/service-linked-role-create-policy.json",
)
}
resource "aws_iam_policy" "service_linked_role" {
count = var.allow_iam_service_linked_role_creation ? 1 : 0
name = "${var.environment}-service_linked_role"
path = "/"
description = "Policy for creation of service linked roles."
policy = data.template_file.service_linked_role[0].rendered
}
resource "aws_iam_role_policy_attachment" "service_linked_role" {
count = var.allow_iam_service_linked_role_creation ? 1 : 0
role = aws_iam_role.instance.name
policy_arn = aws_iam_policy.service_linked_role[0].arn
}
################################################################################
### AWS Systems Manager access to store runner token once registered
################################################################################
data "template_file" "ssm_policy" {
count = var.enable_manage_gitlab_token ? 1 : 0
template = file(
"${path.module}/policies/instance-secure-parameter-role-policy.json",
)
}
resource "aws_iam_policy" "ssm" {
count = var.enable_manage_gitlab_token ? 1 : 0
name = "${var.environment}-ssm"
path = "/"
description = "Policy for runner token param access via SSM"
policy = data.template_file.ssm_policy[0].rendered
}
resource "aws_iam_role_policy_attachment" "ssm" {
count = var.enable_manage_gitlab_token ? 1 : 0
role = aws_iam_role.instance.name
policy_arn = aws_iam_policy.ssm[0].arn
}