-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoscale_group.tf
142 lines (134 loc) · 5.01 KB
/
autoscale_group.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
locals {
executor = (
var.asg.executor == "docker" ?
"--executor 'docker' --docker-image alpine:latest --docker-privileged --description \"docker-runner $${INSTANCE_ID}\"" :
"--executor 'shell' --description \"shell-runner $${INSTANCE_ID}\""
)
}
resource "aws_launch_template" "runner" {
block_device_mappings {
device_name = lookup(var.asg.root_block_device, "name", "/dev/xvda")
ebs {
delete_on_termination = lookup(var.asg.root_block_device, "delete_on_termination", true)
encrypted = lookup(var.asg.root_block_device, "encrypted", true)
iops = lookup(var.asg.root_block_device, "iops", null)
throughput = lookup(var.asg.root_block_device, "throughput", null)
volume_size = lookup(var.asg.root_block_device, "volume_size", null)
volume_type = lookup(var.asg.root_block_device, "volume_type", "gp3")
}
}
iam_instance_profile {
name = aws_iam_instance_profile.runner.name
}
image_id = var.asg.image_id == "" ? data.aws_ami.amazonlinux2[0].id : var.asg.image_id
# Dynamic due to https://github.com/hashicorp/terraform-provider-aws/issues/24009
dynamic "instance_market_options" {
for_each = var.asg.spot_price != null ? [1] : []
content {
market_type = "spot"
spot_options {
max_price = var.asg.spot_price == "" ? null : var.asg.spot_price
spot_instance_type = "one-time"
}
}
}
instance_type = var.asg.instance_type
key_name = var.asg.ssh_access.key_name
name_prefix = "gitlab-runner-"
network_interfaces {
associate_public_ip_address = var.asg.associate_public_ip_address
security_groups = [aws_security_group.runner.id]
}
update_default_version = true
user_data = base64encode(templatefile("${path.module}/templates/user_data/install_runner.sh.tpl", {
executor = local.executor
executor_type = var.asg.executor
gitlab_url = var.gitlab.uri
hookchecker_py_content = templatefile(
"${path.module}/templates/hookchecker/hookchecker.py",
{
LOG_LEVEL = var.asg.log_level,
},
)
hookchecker_service_content = file("${path.module}/templates/hookchecker/hookchecker.service")
log_group = aws_cloudwatch_log_group.runner.name
num_runners = var.gitlab.runner_agents_per_instance
project_id = var.gitlab.project_id
region = data.aws_region.current.name
runner_registration_type = var.gitlab.runner_registration_type
runner_job_tags = local.asg_tag_list
runner_token_ssm_path = var.gitlab.runner_registration_token_ssm_path
runner_version = var.gitlab.runner_version
}))
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "runner" {
desired_capacity = var.asg.desired_capacity != -1 ? var.asg.desired_capacity : null
health_check_grace_period = 120
launch_template {
id = aws_launch_template.runner.id
version = "$Latest"
}
max_size = var.asg.max_size
min_size = var.asg.min_size
name_prefix = "gitlab-runner-"
termination_policies = [
"OldestInstance"
]
vpc_zone_identifier = var.asg.subnet_ids
lifecycle {
create_before_destroy = true
ignore_changes = [
desired_capacity # changes via autoscaling so we need to ignore this on redeploy
]
}
tag {
key = "Name"
value = "gitlab-runner"
propagate_at_launch = true
}
}
resource "aws_autoscaling_lifecycle_hook" "hook" {
autoscaling_group_name = aws_autoscaling_group.runner.name
default_result = "CONTINUE"
heartbeat_timeout = var.asg.force_instance_deletion_time
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
name = "terminate-runner"
}
resource "aws_autoscaling_policy" "gitlab_runners_scale_out" {
adjustment_type = "ChangeInCapacity"
autoscaling_group_name = aws_autoscaling_group.runner.name
estimated_instance_warmup = var.asg.scaling_warmup
name = "scale-out"
policy_type = "StepScaling"
step_adjustment {
scaling_adjustment = 1
metric_interval_lower_bound = 0.1
metric_interval_upper_bound = 2
}
step_adjustment {
scaling_adjustment = 2
metric_interval_lower_bound = 2
}
}
resource "aws_autoscaling_policy" "gitlab_runners_scale_in" {
adjustment_type = "ChangeInCapacity"
autoscaling_group_name = aws_autoscaling_group.runner.name
name = "scale-in"
policy_type = "StepScaling"
step_adjustment {
scaling_adjustment = -1
metric_interval_lower_bound = -79
metric_interval_upper_bound = 0
}
step_adjustment {
scaling_adjustment = -var.asg.max_size
metric_interval_upper_bound = -79
}
}
resource "aws_cloudwatch_log_group" "runner" {
name = "/gitlab/runner/logs-${random_string.rule_suffix.result}"
retention_in_days = 30
}