@@ -171,16 +171,29 @@ data "aws_ami" "docker-machine" {
171
171
}
172
172
173
173
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"
175
175
vpc_zone_identifier = var. subnet_ids_gitlab_runner
176
176
min_size = " 1"
177
177
max_size = " 1"
178
178
desired_capacity = " 1"
179
179
health_check_grace_period = 0
180
- launch_configuration = aws_launch_configuration. gitlab_runner_instance . name
181
180
enabled_metrics = var. metrics_autoscaling
182
181
tags = local. agent_tags_propagated
183
182
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
+
184
197
timeouts {
185
198
delete = var. asg_delete_timeout
186
199
}
@@ -220,34 +233,69 @@ data "aws_ami" "runner" {
220
233
owners = var. ami_owners
221
234
}
222
235
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" {
235
260
for_each = [var . runner_root_block_device ]
236
261
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
242
290
}
243
291
}
292
+ tags = local. tags
293
+
244
294
metadata_options {
245
295
http_endpoint = var. runner_instance_metadata_options_http_endpoint
246
296
http_tokens = var. runner_instance_metadata_options_http_tokens
247
297
}
248
298
249
- associate_public_ip_address = false == var. runners_use_private_address
250
-
251
299
lifecycle {
252
300
create_before_destroy = true
253
301
}
0 commit comments