Skip to content

Commit 1bb7e11

Browse files
coibibmarvin-wkayman-mk
authoredApr 21, 2023
feat(spotfleet): add supports spot fleets for spot instances allowing us to use multiple instance types and AZs (#777)
## Description The [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine used in this module has released a version that supports [AWS Spot Fleet](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html) 3 months ago. In our case, spot fleets are useful when using spot instances because it fixes the issue when there is no capacity available for a certain instance type in a certain AZ. With spot fleets, we can use multiple instance types and AZ so when there is no capacity on one instance type, AWS will take the next instance type and so on. This fixes issues [#76](#76) [#435](#435) [#77](#77) and [#474](#474) ## Migrations required NO our solution is backward compatible so if you don't plan to use spot fleets, no migration is required. YES if you want to use spot fleets you will have to do the following changes: `docker_machine_instance_type` to `docker_machine_instance_types` `subnet_id` to `subnet_ids` You also have to put these 2 new parameters: `use_fleet` and `key_pair_name` ### Important We have seen that the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine this module is using consume more RAM using spot fleets. For comparison, if you launch 50 machines in the same time, it consumes ~1.2GB of RAM. In our case, we had to change the `instance_type` of the runner from `t3.micro` to `t3.small`. ## Verification ```hcl docker_machine_instance_types = ["r5a.xlarge", "r5.xlarge", "r5ad.xlarge", "r5d.xlarge", "m5a.2xlarge"] use_fleet = true key_pair_name = "<key-pair-name>" ``` We are using this solution in production for a week now with 150+ developers and over 20 000 jobs a day without any issue for now. --------- Signed-off-by: François Bibron <[email protected]> Co-authored-by: François Bibron <[email protected]> Co-authored-by: Marvin Wichmann <[email protected]> Co-authored-by: Matthias Kay <[email protected]> Co-authored-by: kayma <[email protected]>
1 parent df25b6a commit 1bb7e11

File tree

7 files changed

+181
-6
lines changed

7 files changed

+181
-6
lines changed
 

‎README.md

+51
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,48 @@ module "runner" {
371371
}
372372
```
373373

374+
### Scenario: Use of Spot Fleet
375+
376+
Since spot instances can be taken over by AWS depending on the instance type and AZ you are using, you may want multiple instances types in multiple AZs. This is where spot fleets come in, when there is no capacity on one instance type and one AZ, AWS will take the next instance type and so on. This update has been possible since the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine supports spot fleets.
377+
378+
We have seen that the [fork](https://gitlab.com/cki-project/docker-machine/-/tree/v0.16.2-gitlab.19-cki.2) of docker-machine this module is using consume more RAM using spot fleets.
379+
For comparison, if you launch 50 machines in the same time, it consumes ~1.2GB of RAM. In our case, we had to change the `instance_type` of the runner from `t3.micro` to `t3.small`.
380+
381+
#### Configuration example
382+
```hcl
383+
module "runner" {
384+
# https://registry.terraform.io/modules/npalm/gitlab-runner/aws/
385+
source = "npalm/gitlab-runner/aws"
386+
387+
aws_region = "eu-west-3"
388+
environment = "spot-runners"
389+
390+
vpc_id = module.vpc.vpc_id
391+
subnet_id = module.vpc.private_subnets[0] # subnet of the agent
392+
fleet_executor_subnet_ids = module.vpc.private_subnets
393+
394+
docker_machine_instance_types = ["t3a.medium", "t3.medium", "t2.medium"]
395+
use_fleet = true
396+
key_pair_name = "<key_pair_name>"
397+
398+
runners_name = "docker-machine"
399+
runners_gitlab_url = "https://gitlab.com"
400+
401+
gitlab_runner_registration_config = {
402+
registration_token = "my-token"
403+
tag_list = "docker"
404+
description = "runner default"
405+
locked_to_project = "true"
406+
run_untagged = "false"
407+
maximum_timeout = "3600"
408+
}
409+
410+
overrides = {
411+
name_iam_objects = "<region-specific-prefix>-gitlab-runner-iam"
412+
}
413+
}
414+
```
415+
374416
## Examples
375417

376418
A few [examples](https://github.com/cattle-ops/terraform-aws-gitlab-runner/tree/main/examples/) are provided. Use the
@@ -435,13 +477,15 @@ Made with [contributors-img](https://contrib.rocks).
435477
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1 |
436478
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4 |
437479
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 2.4.0 |
480+
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | >= 3 |
438481

439482
## Providers
440483

441484
| Name | Version |
442485
|------|---------|
443486
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.49.0 |
444487
| <a name="provider_local"></a> [local](#provider\_local) | >= 2.4.0 |
488+
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 3 |
445489

446490
## Modules
447491

@@ -479,9 +523,11 @@ Made with [contributors-img](https://contrib.rocks).
479523
| [aws_iam_role_policy_attachment.service_linked_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
480524
| [aws_iam_role_policy_attachment.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
481525
| [aws_iam_role_policy_attachment.user_defined_policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
526+
| [aws_key_pair.fleet_key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/key_pair) | resource |
482527
| [aws_kms_alias.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
483528
| [aws_kms_key.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
484529
| [aws_launch_template.gitlab_runner_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
530+
| [aws_launch_template.gitlab_runners](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
485531
| [aws_security_group.docker_machine](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
486532
| [aws_security_group.runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
487533
| [aws_security_group_rule.docker_machine_docker_runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
@@ -495,6 +541,7 @@ Made with [contributors-img](https://contrib.rocks).
495541
| [aws_ssm_parameter.runner_sentry_dsn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
496542
| [local_file.config_toml](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
497543
| [local_file.user_data](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
544+
| [tls_private_key.runner](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource |
498545
| [aws_ami.docker-machine](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
499546
| [aws_ami.runner](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
500547
| [aws_availability_zone.runners](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zone) | data source |
@@ -537,6 +584,7 @@ Made with [contributors-img](https://contrib.rocks).
537584
| <a name="input_docker_machine_iam_policy_arns"></a> [docker\_machine\_iam\_policy\_arns](#input\_docker\_machine\_iam\_policy\_arns) | List of policy ARNs to be added to the instance profile of the docker machine runners. | `list(string)` | `[]` | no |
538585
| <a name="input_docker_machine_instance_metadata_options"></a> [docker\_machine\_instance\_metadata\_options](#input\_docker\_machine\_instance\_metadata\_options) | Enable the docker machine instances metadata service. Requires you use GitLab maintained docker machines. | <pre>object({<br> http_tokens = string<br> http_put_response_hop_limit = number<br> })</pre> | <pre>{<br> "http_put_response_hop_limit": 2,<br> "http_tokens": "required"<br>}</pre> | no |
539586
| <a name="input_docker_machine_instance_type"></a> [docker\_machine\_instance\_type](#input\_docker\_machine\_instance\_type) | Instance type used for the instances hosting docker-machine. | `string` | `"m5.large"` | no |
587+
| <a name="input_docker_machine_instance_types"></a> [docker\_machine\_instance\_types](#input\_docker\_machine\_instance\_types) | Instance types used for the instances hosting docker-machine. This variable is only supported when use\_fleet is set to true. | `list(string)` | `[]` | no |
540588
| <a name="input_docker_machine_options"></a> [docker\_machine\_options](#input\_docker\_machine\_options) | List of additional options for the docker machine config. Each element of this list must be a key=value pair. E.g. '["amazonec2-zone=a"]' | `list(string)` | `[]` | no |
541589
| <a name="input_docker_machine_role_json"></a> [docker\_machine\_role\_json](#input\_docker\_machine\_role\_json) | Docker machine runner instance override policy, expected to be in JSON format. | `string` | `""` | no |
542590
| <a name="input_docker_machine_security_group_description"></a> [docker\_machine\_security\_group\_description](#input\_docker\_machine\_security\_group\_description) | A description for the docker-machine security group | `string` | `"A security group containing docker-machine instances"` | no |
@@ -554,13 +602,15 @@ Made with [contributors-img](https://contrib.rocks).
554602
| <a name="input_enable_schedule"></a> [enable\_schedule](#input\_enable\_schedule) | Flag used to enable/disable auto scaling group schedule for the runner instance. | `bool` | `false` | no |
555603
| <a name="input_environment"></a> [environment](#input\_environment) | A name that identifies the environment, used as prefix and for tagging. | `string` | n/a | yes |
556604
| <a name="input_extra_security_group_ids_runner_agent"></a> [extra\_security\_group\_ids\_runner\_agent](#input\_extra\_security\_group\_ids\_runner\_agent) | Optional IDs of extra security groups to apply to the runner agent. This will not apply to the runners spun up when using the docker+machine executor, which is the default. | `list(string)` | `[]` | no |
605+
| <a name="input_fleet_executor_subnet_ids"></a> [fleet\_executor\_subnet\_ids](#input\_fleet\_executor\_subnet\_ids) | List of subnets used for executors when the fleet mode is enabled. Must belong to the VPC specified above. | `list(string)` | `[]` | no |
557606
| <a name="input_gitlab_runner_egress_rules"></a> [gitlab\_runner\_egress\_rules](#input\_gitlab\_runner\_egress\_rules) | List of egress rules for the gitlab runner instance. | <pre>list(object({<br> cidr_blocks = list(string)<br> ipv6_cidr_blocks = list(string)<br> prefix_list_ids = list(string)<br> from_port = number<br> protocol = string<br> security_groups = list(string)<br> self = bool<br> to_port = number<br> description = string<br> }))</pre> | <pre>[<br> {<br> "cidr_blocks": [<br> "0.0.0.0/0"<br> ],<br> "description": null,<br> "from_port": 0,<br> "ipv6_cidr_blocks": [<br> "::/0"<br> ],<br> "prefix_list_ids": null,<br> "protocol": "-1",<br> "security_groups": null,<br> "self": null,<br> "to_port": 0<br> }<br>]</pre> | no |
558607
| <a name="input_gitlab_runner_registration_config"></a> [gitlab\_runner\_registration\_config](#input\_gitlab\_runner\_registration\_config) | Configuration used to register the runner. See the README for an example, or reference the examples in the examples directory of this repo. | `map(string)` | <pre>{<br> "access_level": "",<br> "description": "",<br> "locked_to_project": "",<br> "maximum_timeout": "",<br> "registration_token": "",<br> "run_untagged": "",<br> "tag_list": ""<br>}</pre> | no |
559608
| <a name="input_gitlab_runner_security_group_description"></a> [gitlab\_runner\_security\_group\_description](#input\_gitlab\_runner\_security\_group\_description) | A description for the gitlab-runner security group | `string` | `"A security group containing gitlab-runner agent instances"` | no |
560609
| <a name="input_gitlab_runner_security_group_ids"></a> [gitlab\_runner\_security\_group\_ids](#input\_gitlab\_runner\_security\_group\_ids) | A list of security group ids that are allowed to access the gitlab runner agent | `list(string)` | `[]` | no |
561610
| <a name="input_gitlab_runner_version"></a> [gitlab\_runner\_version](#input\_gitlab\_runner\_version) | Version of the [GitLab runner](https://gitlab.com/gitlab-org/gitlab-runner/-/releases). | `string` | `"15.8.2"` | no |
562611
| <a name="input_instance_role_json"></a> [instance\_role\_json](#input\_instance\_role\_json) | Default runner instance override policy, expected to be in JSON format. | `string` | `""` | no |
563612
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Instance type used for the GitLab runner. | `string` | `"t3.micro"` | no |
613+
| <a name="input_key_pair_name"></a> [key\_pair\_name](#input\_key\_pair\_name) | The name of the key pair used by the runner to connect to the docker-machine executors. | `string` | `"fleet-key"` | no |
564614
| <a name="input_kms_alias_name"></a> [kms\_alias\_name](#input\_kms\_alias\_name) | Alias added to the kms\_key (if created and not provided by kms\_key\_id) | `string` | `""` | no |
565615
| <a name="input_kms_deletion_window_in_days"></a> [kms\_deletion\_window\_in\_days](#input\_kms\_deletion\_window\_in\_days) | Key rotation window, set to 0 for no rotation. Only used when `enable_kms` is set to `true`. | `number` | `7` | no |
566616
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | KMS key id to encrypted the resources. Ensure CloudWatch and Runner/Executor have access to the provided KMS key. | `string` | `""` | no |
@@ -637,6 +687,7 @@ Made with [contributors-img](https://contrib.rocks).
637687
| <a name="input_subnet_ids_gitlab_runner"></a> [subnet\_ids\_gitlab\_runner](#input\_subnet\_ids\_gitlab\_runner) | Deprecated! Use subnet\_id instead. Subnet used for hosting the GitLab runner. | `list(string)` | `[]` | no |
638688
| <a name="input_suppressed_tags"></a> [suppressed\_tags](#input\_suppressed\_tags) | List of tag keys which are removed from tags, agent\_tags and runner\_tags and never added as default tag by the module. | `list(string)` | `[]` | no |
639689
| <a name="input_tags"></a> [tags](#input\_tags) | Map of tags that will be added to created resources. By default resources will be tagged with name and environment. | `map(string)` | `{}` | no |
690+
| <a name="input_use_fleet"></a> [use\_fleet](#input\_use\_fleet) | Use the fleet mode for agents. https://gitlab.com/cki-project/docker-machine/-/blob/v0.16.2-gitlab.19-cki.2/docs/drivers/aws.md#fleet-mode | `bool` | `false` | no |
640691
| <a name="input_userdata_post_install"></a> [userdata\_post\_install](#input\_userdata\_post\_install) | User-data script snippet to insert after GitLab runner install | `string` | `""` | no |
641692
| <a name="input_userdata_pre_install"></a> [userdata\_pre\_install](#input\_userdata\_pre\_install) | User-data script snippet to insert before GitLab runner install | `string` | `""` | no |
642693
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The target VPC for the docker-machine and runner instances. | `string` | n/a | yes |

‎main.tf

+79-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ locals {
7777
gitlab_runner_maximum_timeout = var.gitlab_runner_registration_config["maximum_timeout"]
7878
gitlab_runner_access_level = lookup(var.gitlab_runner_registration_config, "access_level", "not_protected")
7979
sentry_dsn = var.sentry_dsn
80+
public_key = var.use_fleet == true ? tls_private_key.fleet[0].public_key_openssh : ""
81+
use_fleet = var.use_fleet
82+
private_key = var.use_fleet == true ? tls_private_key.fleet[0].private_key_pem : ""
8083
})
8184

8285
template_runner_config = templatefile("${path.module}/template/runner-config.tftpl",
@@ -88,8 +91,10 @@ locals {
8891
runners_extra_hosts = var.runners_extra_hosts
8992
runners_vpc_id = var.vpc_id
9093
runners_subnet_id = length(var.subnet_id) > 0 ? var.subnet_id : var.subnet_id_runners
94+
runners_subnet_ids = length(var.fleet_executor_subnet_ids) > 0 ? var.fleet_executor_subnet_ids : length(var.subnet_id) > 0 ? [var.subnet_id] : [var.subnet_id_runners]
9195
runners_aws_zone = data.aws_availability_zone.runners.name_suffix
9296
runners_instance_type = var.docker_machine_instance_type
97+
runners_instance_types = length(var.docker_machine_instance_types_fleet) > 0 ? var.docker_machine_instance_types_fleet : [var.docker_machine_instance_type]
9398
runners_spot_price_bid = var.docker_machine_spot_price_bid == "on-demand-price" || var.docker_machine_spot_price_bid == null ? "" : var.docker_machine_spot_price_bid
9499
runners_ami = var.runners_executor == "docker+machine" ? data.aws_ami.docker-machine[0].id : ""
95100
runners_security_group_name = var.runners_executor == "docker+machine" ? aws_security_group.docker_machine[0].name : ""
@@ -138,6 +143,8 @@ locals {
138143
sentry_dsn = var.sentry_dsn
139144
prometheus_listen_address = var.prometheus_listen_address
140145
auth_type = var.auth_type_cache_sr
146+
use_fleet = var.use_fleet
147+
launch_template = var.use_fleet == true ? aws_launch_template.fleet_gitlab_runner[0].name : ""
141148
}
142149
)
143150
}
@@ -163,7 +170,7 @@ data "aws_ami" "docker-machine" {
163170
# kics-scan ignore-line
164171
resource "aws_autoscaling_group" "gitlab_runner_instance" {
165172
name = var.enable_asg_recreation ? "${aws_launch_template.gitlab_runner_instance.name}-asg" : "${var.environment}-as-group"
166-
vpc_zone_identifier = length(var.subnet_id) > 0 ? [var.subnet_id] : var.subnet_ids_gitlab_runner
173+
vpc_zone_identifier = length(var.fleet_executor_subnet_ids) > 0 ? var.fleet_executor_subnet_ids : length(var.subnet_id) > 0 ? [var.subnet_id] : var.subnet_ids_gitlab_runner
167174
min_size = "1"
168175
max_size = "1"
169176
desired_capacity = "1"
@@ -319,6 +326,77 @@ resource "aws_launch_template" "gitlab_runner_instance" {
319326
depends_on = [aws_cloudwatch_log_group.environment]
320327
}
321328

329+
resource "tls_private_key" "fleet" {
330+
count = var.use_fleet == true && var.runners_executor == "docker+machine" ? 1 : 0
331+
332+
algorithm = "RSA"
333+
rsa_bits = 4096
334+
}
335+
336+
resource "aws_key_pair" "fleet" {
337+
count = var.use_fleet == true && var.runners_executor == "docker+machine" ? 1 : 0
338+
339+
key_name = "${var.environment}-${var.fleet_key_pair_name}"
340+
public_key = tls_private_key.fleet[0].public_key_openssh
341+
342+
tags = local.tags
343+
}
344+
345+
resource "aws_launch_template" "fleet_gitlab_runner" {
346+
# checkov:skip=CKV_AWS_88:User can decide to add a public IP.
347+
# checkov:skip=CKV_AWS_79:User can decide to enable Metadata service V2. V2 is the default.
348+
count = var.use_fleet == true && var.runners_executor == "docker+machine" ? 1 : 0
349+
name_prefix = "${local.name_runner_agent_instance}-worker-"
350+
351+
key_name = aws_key_pair.fleet[0].key_name
352+
image_id = data.aws_ami.docker-machine[0].id
353+
user_data = base64gzip(var.runners_userdata)
354+
instance_type = var.docker_machine_instance_types_fleet[0] # it will be override by the fleet
355+
update_default_version = true
356+
ebs_optimized = var.runners_ebs_optimized
357+
monitoring {
358+
enabled = var.runners_monitoring
359+
}
360+
block_device_mappings {
361+
device_name = "/dev/sda1"
362+
363+
ebs {
364+
volume_size = var.runners_root_size
365+
volume_type = var.runners_volume_type
366+
}
367+
}
368+
369+
iam_instance_profile {
370+
name = aws_iam_instance_profile.docker_machine[0].name
371+
}
372+
373+
network_interfaces {
374+
security_groups = [aws_security_group.docker_machine[0].id]
375+
associate_public_ip_address = !var.runners_use_private_address
376+
}
377+
378+
tag_specifications {
379+
resource_type = "instance"
380+
tags = local.tags
381+
}
382+
tag_specifications {
383+
resource_type = "volume"
384+
tags = local.tags
385+
}
386+
387+
tags = local.tags
388+
389+
metadata_options {
390+
http_tokens = var.docker_machine_instance_metadata_options.http_tokens
391+
http_put_response_hop_limit = var.docker_machine_instance_metadata_options.http_put_response_hop_limit
392+
instance_metadata_tags = "enabled"
393+
}
394+
395+
lifecycle {
396+
create_before_destroy = true
397+
}
398+
}
399+
322400
################################################################################
323401
### Create cache bucket
324402
################################################################################

‎policies/instance-docker-machine-policy.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"ec2:RequestSpotInstances",
1818
"ec2:CancelSpotInstanceRequests",
1919
"ec2:DescribeSubnets",
20-
"ec2:AssociateIamInstanceProfile"
20+
"ec2:AssociateIamInstanceProfile",
21+
"ec2:CreateFleet"
2122
],
2223
"Effect": "Allow",
2324
"Resource": "*"

‎template/gitlab-runner.tftpl

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ fi
4848

4949
sed -i.bak s/__REPLACED_BY_USER_DATA__/$token/g /etc/gitlab-runner/config.toml
5050

51+
if [[ "${use_fleet}" == "true" ]]
52+
then
53+
echo "${public_key}" > /root/.ssh/id_rsa.pub
54+
echo "${private_key}" > /root/.ssh/id_rsa
55+
56+
chmod 600 /root/.ssh/id_rsa
57+
fi
58+
5159
ssm_sentry_dsn=$(aws ssm get-parameters --names "${secure_parameter_store_runner_sentry_dsn}" --with-decryption --region "${secure_parameter_store_region}" | jq -r ".Parameters | .[0] | .Value")
5260
if [[ "${sentry_dsn}" == "__SENTRY_DSN_REPLACED_BY_USER_DATA__" && "$ssm_sentry_dsn" == "null" ]]
5361
then

‎template/runner-config.tftpl

+13-4
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,33 @@ listen_address = "${prometheus_listen_address}"
5050
MachineDriver = "amazonec2"
5151
MachineName = "${docker_machine_name}"
5252
MachineOptions = [
53-
"amazonec2-instance-type=${runners_instance_type}",
53+
%{~ for instance_type in runners_instance_types ~}
54+
"amazonec2-instance-type=${instance_type}",
55+
%{~ endfor ~}
5456
"amazonec2-region=${aws_region}",
5557
"amazonec2-zone=${runners_aws_zone}",
5658
"amazonec2-vpc-id=${runners_vpc_id}",
57-
"amazonec2-subnet-id=${runners_subnet_id}",
59+
%{~ for subnet_id in runners_subnet_ids ~}
60+
"amazonec2-subnet-id=${subnet_id}",
61+
%{~ endfor ~}
5862
"amazonec2-private-address-only=${runners_use_private_address_only}",
5963
"amazonec2-use-private-address=${runners_use_private_address}",
6064
"amazonec2-request-spot-instance=${runners_request_spot_instance}",
61-
%{ if runners_spot_price_bid != ""}"amazonec2-spot-price=${runners_spot_price_bid}",%{ endif ~}
65+
%{~ if runners_spot_price_bid != "" ~}"amazonec2-spot-price=${runners_spot_price_bid}",%{~ endif ~}
6266
"amazonec2-security-group=${runners_security_group_name}",
6367
"amazonec2-tags=${runners_tags},__PARENT_TAG__",
6468
"amazonec2-use-ebs-optimized-instance=${runners_ebs_optimized}",
6569
"amazonec2-monitoring=${runners_monitoring}",
6670
"amazonec2-iam-instance-profile=%{ if runners_iam_instance_profile_name != "" }${runners_iam_instance_profile_name}%{ else }${runners_instance_profile}%{ endif ~}",
6771
"amazonec2-root-size=${runners_root_size}",
6872
"amazonec2-volume-type=${runners_volume_type}",
69-
"amazonec2-userdata=%{ if runners_userdata != "" }/etc/gitlab-runner/runners_userdata.sh%{ endif ~}",
73+
"amazonec2-userdata=%{~ if runners_userdata != "" ~}/etc/gitlab-runner/runners_userdata.sh%{~ endif ~}",
7074
"amazonec2-ami=${runners_ami}"
75+
%{~ if use_fleet == true ~}
76+
,"amazonec2-ssh-keypath=/root/.ssh/id_rsa",
77+
"amazonec2-use-fleet=${use_fleet}",
78+
"amazonec2-launch-template=${launch_template}"
79+
%{~ endif ~}
7180
${docker_machine_options}
7281
]
7382

‎variables.tf

+24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ variable "subnet_id" {
3131
default = "" # TODO remove as soon as subnet_id_runners and subnet_ids_gitlab_runner are gone. Variable is mandatory now.
3232
}
3333

34+
variable "fleet_executor_subnet_ids" {
35+
description = "List of subnets used for executors when the fleet mode is enabled. Must belong to the VPC specified above."
36+
type = list(string)
37+
default = []
38+
}
39+
3440
variable "extra_security_group_ids_runner_agent" {
3541
description = "Optional IDs of extra security groups to apply to the runner agent. This will not apply to the runners spun up when using the docker+machine executor, which is the default."
3642
type = list(string)
@@ -43,6 +49,12 @@ variable "metrics_autoscaling" {
4349
default = null
4450
}
4551

52+
variable "fleet_key_pair_name" {
53+
description = "The name of the key pair used by the runner to connect to the docker-machine executors."
54+
type = string
55+
default = "fleet-key"
56+
}
57+
4658
variable "instance_type" {
4759
description = "Instance type used for the GitLab runner."
4860
type = string
@@ -101,6 +113,12 @@ variable "docker_machine_instance_type" {
101113
default = "m5.large"
102114
}
103115

116+
variable "docker_machine_instance_types_fleet" {
117+
description = "Instance types used for the instances hosting docker-machine. This variable is only supported when use_fleet is set to true."
118+
type = list(string)
119+
default = []
120+
}
121+
104122
variable "docker_machine_spot_price_bid" {
105123
description = "Spot price bid. The maximum price willing to pay. By default the price is limited by the current on demand price for the instance type chosen."
106124
type = string
@@ -763,6 +781,12 @@ variable "kms_deletion_window_in_days" {
763781
default = 7
764782
}
765783

784+
variable "use_fleet" {
785+
description = "Use the fleet mode for agents. https://gitlab.com/cki-project/docker-machine/-/blob/v0.16.2-gitlab.19-cki.2/docs/drivers/aws.md#fleet-mode"
786+
type = bool
787+
default = false
788+
}
789+
766790
variable "enable_eip" {
767791
description = "Enable the assignment of an EIP to the gitlab runner instance"
768792
default = false

‎versions.tf

+4
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ terraform {
1010
source = "hashicorp/local"
1111
version = ">= 2.4.0"
1212
}
13+
tls = {
14+
source = "hashicorp/tls"
15+
version = ">= 3"
16+
}
1317
}
1418
}

0 commit comments

Comments
 (0)
Please sign in to comment.