module-base-gitlab-runner-manager

This module creates a GitLab CI Runner Manager. The module will spawn a single EC2 node in a self-healing AutoScaling Group and runners will be spawned by docker-machine using spot instances. Runners will scale automatically based on the configuration. The module also creates by default a S3 cache bucket that is shared across spot instances runners.

Usage examples

Simple example

module "gitlab_runners" {
  source = "git::https://gitlab.vectoritcgroup.com/vectordigital/iac/terraform/modules/aws/compute/module-base-gitlab-runner-manager.git?ref=vX.Y.Z"

  vpc_id      = var.vpc_id
  vpc_subnets = var.vpc_subnets

  runners_name      = "aws-spot-runners"
  gitlab_server_url = "https://gitlab.com"

  gitlab_runner_registration_config = {
    registration_token = "abc123xxx456def"
    tag_list           = "tag1,tag2"
    description        = "AWS AutoScaled Spot GitLab Runners"
    locked_to_project  = "false"
    run_untagged       = "true"
    maximum_timeout    = "3600"
  }

  tags = {
    Project     = "project_name"
    Environment = "environment_name"
  }
}

Complex example

module "gitlab_runners" {
  source = "git::https://gitlab.vectoritcgroup.com/vectordigital/iac/terraform/modules/aws/compute/module-base-gitlab-runner-manager.git?ref=vX.Y.Z"

  vpc_id      = var.vpc_id
  vpc_subnets = var.vpc_subnets

  runners_name      = "aws-spot-runners"
  gitlab_server_url = "https://gitlab.com"

  gitlab_runner_registration_config = {
    registration_token = "abc123xxx456def"
    tag_list           = "tag1,tag2"
    description        = "AWS AutoScaled Spot GitLab Runners"
    locked_to_project  = "false"
    run_untagged       = "true"
    maximum_timeout    = "3600"
  }

  runners_limit: 20
  runners_concurrent: 20
  runners_idle_time: 600
  runners_idle_count: 1

  runners_off_peak_timezone: "Europe/Madrid"
  runners_off_peak_periods: "[\"* * 0-9,17-23 * * mon-fri *\", \"* * * * * sat,sun *\"]"
  runners_off_peak_idle_count: 0
  runners_off_peak_idle_time: 60

  runners_instance_type: "m5.large"
  runners_spot_price_bid: "0.035"
  runners_root_size: 20
  runners_request_concurrency: 20
  runners_output_limit: 524288

  runners_cache_enabled: true
  runners_cache_expiration_days: 7

  tags = {
    Project     = "project_name"
    Environment = "environment_name"
  }
}

Module argument reference

Modules

Name Source Version

asg

git::https://gitlab.vectoritcgroup.com/vectordigital/iac/terraform/modules/aws/compute/module-base-asg.git

v0.1.0

git::https://gitlab.vectoritcgroup.com/vectordigital/iac/terraform/modules/aws/networking/module-base-sg.git

v0.2.2

git::https://gitlab.vectoritcgroup.com/vectordigital/iac/terraform/modules/aws/networking/module-base-sg.git

v0.2.2

Inputs

Name Description Type Default Required

AWS Region name

string

n/a

yes

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)

{
  "access_level": "",
  "description": "",
  "locked_to_project": "",
  "maximum_timeout": "",
  "registration_token": "",
  "run_untagged": "",
  "tag_list": ""
}

no

URL of the gitlab instance to connect to.

string

n/a

yes

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

Version of the docker-machine package to install in the Gitlab Runner Manager instance

string

"0.16.2"

no

The executor to use in the Gitlab Runner Manager. It can be docker+machine or docker

string

"docker+machine"

no

Boolean used to enable or disable the CloudWatch logging.

bool

true

no

Let the module manage a KMS key, logs will be encrypted via KMS. Be-aware of the costs of an custom key.

bool

false

no

KMS key id to encrypted the CloudWatch logs. Ensure CloudWatch has access to the provided KMS key.

string

""

no

Key rotation window, set to 0 for no rotation. Only used when logging_kms_enabled is set to true.

number

7

no

Retention for cloudwatch logs. Defaults to unlimited

number

0

no

Version of the Gitlab Runner to be installed the Gitlab Runner Manager instance

string

"12.8.0"

no

User-data script snippet to insert after GitLab Runner Manager installation

string

""

no

User-data script snippet to insert before GitLab Runner Manager installation

string

""

no

The service name

string

"gitlab-runner-manager"

no

The prefix to be attached to every resource name

string

n/a

yes

Additional volumes that will be used in the runner config.toml, e.g Docker socket

list(string)

[]

no

List of maps used to create the AMI filter for the Gitlab Runner AMI

map(list(string))

{
  "name": [
    "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"
  ]
}

no

The list of owners used to select the AMI of Gitlab Runner instance.

list(string)

[
  "099720109477"
]

no

Wether or not to create an S3 bucket for storing Gitlab Runners cache

bool

true

no

Number of days before the Gitlab Runners cache objects expires

number

1

no

Concurrent value for the runners, will be used in the runner config.toml.

number

10

no

Environment variables during build execution, e.g. KEY=Value, see runner-public example. Will be used in the runner config.toml

list(string)

[]

no

The executor to use. Currently supports docker+machine or docker.

string

"docker+machine"

no

IAM instance profile name of the runners, will be used in the runner config.toml

string

""

no

Idle count of the runners, will be used in the runner config.toml.

number

0

no

Idle time of the runners, will be used in the runner config.toml.

number

1800

no

Image to run builds, will be used in the runner config.toml

string

"docker:19.03.1"

no

Instance type used for the instances hosting docker-machine.

string

"t3.large"

no

Limit for the runners, will be used in the runner config.toml.

number

0

no

Max builds for each runner after which it will be removed, will be used in the runner config.toml. By default set to 0, no maxBuilds will be set in the configuration.

number

0

no

Name of the runner, will be used in the runner config.toml.

string

n/a

yes

Off peak idle count of the runners, will be used in the runner config.toml.

number

0

no

Off peak idle time of the runners, will be used in the runner config.toml.

number

0

no

Off peak periods of the runners, will be used in the runner config.toml.

string

""

no

Off peak idle time zone of the runners, will be used in the runner config.toml.

string

""

no

Sets the maximum build log size in kilobytes

number

524288

no

Runners will run in privileged mode, will be used in the runner config.toml

bool

true

no

pull_policy for the runners, will be used in the runner config.toml

string

"always"

no

Limit number of concurrent requests for new jobs from GitLab

number

20

no

Whether or not to request spot instances via docker-machine

bool

true

no

Runner instance root size in GB.

number

16

no

shm_size for the runners, will be used in the runner config.toml

number

0

no

Spot price bid.

string

"0.03"

no

Token for the runner, will be used in the runner config.toml.

string

"REPLACED_BY_USER_DATA"

no

Specific tags for all module resources

map(string)

n/a

yes

The VPC ID of the VPC used to deploy the Gitlab Runner Manager

string

n/a

yes

A list of subnets where the Gitlab Runner Manager ASG will be deployed

list(string)

n/a

yes

Outputs

Name Description

The GitLab Runners S3 cache bucket ARN

The GitLab Runners S3 cache bucket name

The GitLab Runner Manager log group name

The GitLab Runner Manager AutoScaling Group ARN

The GitLab Runner Manager AutoScaling Group name

The GitLab Runner Manager Launch Template ARN

The GitLab Runner Manager Launch Template name

The GitLab Runner Manager security group arn

The GitLab Runner Manager security group id

The GitLab Runner Manager security group name

The GitLab Runners security group arn

The GitLab Runners security group id

The GitLab Runners security group name