Skip to content

Commit b805fb6

Browse files
MichenuxnpalmLaurent Michenaud
authored
feat: replace launch configuration with launch template (#337)
Co-authored-by: Niek Palm <[email protected]> Co-authored-by: Laurent Michenaud <[email protected]>
1 parent 2a07466 commit b805fb6

File tree

2 files changed

+70
-22
lines changed

2 files changed

+70
-22
lines changed

main.tf

+69-21
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,29 @@ data "aws_ami" "docker-machine" {
171171
}
172172

173173
resource "aws_autoscaling_group" "gitlab_runner_instance" {
174-
name = local.enable_asg_recreation ? "${aws_launch_configuration.gitlab_runner_instance.name}-asg" : "${var.environment}-as-group"
174+
name = local.enable_asg_recreation ? "${aws_launch_template.gitlab_runner_instance.name}-asg" : "${var.environment}-as-group"
175175
vpc_zone_identifier = var.subnet_ids_gitlab_runner
176176
min_size = "1"
177177
max_size = "1"
178178
desired_capacity = "1"
179179
health_check_grace_period = 0
180-
launch_configuration = aws_launch_configuration.gitlab_runner_instance.name
181180
enabled_metrics = var.metrics_autoscaling
182181
tags = local.agent_tags_propagated
183182

183+
184+
launch_template {
185+
id = aws_launch_template.gitlab_runner_instance.id
186+
version = aws_launch_template.gitlab_runner_instance.latest_version
187+
}
188+
189+
instance_refresh {
190+
strategy = "Rolling"
191+
preferences {
192+
min_healthy_percentage = 0
193+
}
194+
triggers = ["tag"]
195+
}
196+
184197
timeouts {
185198
delete = var.asg_delete_timeout
186199
}
@@ -220,34 +233,69 @@ data "aws_ami" "runner" {
220233
owners = var.ami_owners
221234
}
222235

223-
resource "aws_launch_configuration" "gitlab_runner_instance" {
224-
name_prefix = var.runners_name
225-
security_groups = [aws_security_group.runner.id]
226-
key_name = var.ssh_key_pair
227-
image_id = data.aws_ami.runner.id
228-
user_data = local.template_user_data
229-
instance_type = var.instance_type
230-
ebs_optimized = var.runner_instance_ebs_optimized
231-
enable_monitoring = var.runner_instance_enable_monitoring
232-
spot_price = var.runner_instance_spot_price
233-
iam_instance_profile = aws_iam_instance_profile.instance.name
234-
dynamic "root_block_device" {
236+
resource "aws_launch_template" "gitlab_runner_instance" {
237+
name_prefix = var.runners_name
238+
key_name = var.ssh_key_pair
239+
image_id = data.aws_ami.runner.id
240+
user_data = base64encode(local.template_user_data)
241+
instance_type = var.instance_type
242+
update_default_version = true
243+
ebs_optimized = var.runner_instance_ebs_optimized
244+
monitoring {
245+
enabled = var.runner_instance_enable_monitoring
246+
}
247+
dynamic "instance_market_options" {
248+
for_each = var.runner_instance_spot_price == null || var.runner_instance_spot_price == "" ? [] : ["spot"]
249+
content {
250+
market_type = instance_market_options.value
251+
spot_options {
252+
max_price = var.runner_instance_spot_price
253+
}
254+
}
255+
}
256+
iam_instance_profile {
257+
name = aws_iam_instance_profile.instance.name
258+
}
259+
dynamic "block_device_mappings" {
235260
for_each = [var.runner_root_block_device]
236261
content {
237-
delete_on_termination = lookup(root_block_device.value, "delete_on_termination", true)
238-
volume_type = lookup(root_block_device.value, "volume_type", "gp3")
239-
volume_size = lookup(root_block_device.value, "volume_size", 8)
240-
encrypted = lookup(root_block_device.value, "encrypted", true)
241-
iops = lookup(root_block_device.value, "iops", null)
262+
device_name = lookup(block_device_mappings.value, "device_name", "/dev/xvda")
263+
ebs {
264+
delete_on_termination = lookup(block_device_mappings.value, "delete_on_termination", true)
265+
volume_type = lookup(block_device_mappings.value, "volume_type", "gp3")
266+
volume_size = lookup(block_device_mappings.value, "volume_size", 8)
267+
encrypted = lookup(block_device_mappings.value, "encrypted", true)
268+
iops = lookup(block_device_mappings.value, "iops", null)
269+
kms_key_id = lookup(block_device_mappings.value, "`kms_key_id`", null)
270+
}
271+
}
272+
}
273+
network_interfaces {
274+
security_groups = [aws_security_group.runner.id]
275+
associate_public_ip_address = false == var.runners_use_private_address
276+
}
277+
tag_specifications {
278+
resource_type = "instance"
279+
tags = local.tags
280+
}
281+
tag_specifications {
282+
resource_type = "volume"
283+
tags = local.tags
284+
}
285+
dynamic "tag_specifications" {
286+
for_each = var.runner_instance_spot_price == null || var.runner_instance_spot_price == "" ? [] : ["spot"]
287+
content {
288+
resource_type = "spot-instances-request"
289+
tags = local.tags
242290
}
243291
}
292+
tags = local.tags
293+
244294
metadata_options {
245295
http_endpoint = var.runner_instance_metadata_options_http_endpoint
246296
http_tokens = var.runner_instance_metadata_options_http_tokens
247297
}
248298

249-
associate_public_ip_address = false == var.runners_use_private_address
250-
251299
lifecycle {
252300
create_before_destroy = true
253301
}

variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ variable "schedule_config" {
607607
}
608608

609609
variable "runner_root_block_device" {
610-
description = "The EC2 instance root block device configuration. Takes the following keys: `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops`"
610+
description = "The EC2 instance root block device configuration. Takes the following keys: `device_name`, `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops`, `kms_key_id`"
611611
type = map(string)
612612
default = {}
613613
}

0 commit comments

Comments
 (0)