### Initialize Terraform Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/docs/usage.md Run this command in your project directory to download and install the necessary Terraform providers and modules. ```sh terraform init ``` -------------------------------- ### Build Ubuntu AMI with Docker Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/packer_images/README.md Use this command to build an Ubuntu AMI with Docker installed. The `docker_registry_mirror` argument is optional. ```bash packer build -var 'vpc_id=your_vpc_id' -var 'subnet_id=your_subnet_id' -var 'docker_registry_mirror=docker-registry-url' ubuntu-docker.json ``` -------------------------------- ### Configure Runner Installation Inputs Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/README.md Defines the configuration for installing the GitLab Runner, including options for ECR credential helper, Docker Machine download URL and version, pre/post install scripts, start script, and yum update. ```terraform object({ amazon_ecr_credential_helper = optional(bool, false) docker_machine_download_url = optional(string, "") docker_machine_version = optional(string, "0.16.2-gitlab.19-cki.5") pre_install_script = optional(string, "") post_install_script = optional(string, "") start_script = optional(string, "") yum_update = optional(bool, true) }) ``` -------------------------------- ### Full Example with Lifecycle Hook Enabled Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/modules/terminate-agent-hook/README.md Demonstrates using the parent module with the lifecycle hook enabled. Pay attention to the `asg_terminate_lifecycle_hook_*` variables. ```terraform module "runner" { source = "cattle-ops/gitlab-runner/aws" environment = "runners-test" vpc_id = module.vpc.vpc_id subnet_id = data.aws_subnet.runner.id runner_gitlab = { url = "https://gitlab.com" access_token_secure_parameter_store_name = aws_ssm_parameter.gitlab_registration_token.value } runner_instance = { name = "runners-test" max_lifetime_seconds = 604800 } runner_worker_docker_machine_instance = { types = ["t3.large"] } runner_worker_docker_machine_instance_spot = { enable = false } runner_worker_docker_machine_role = { profile_name = foo-gitlab-runner-profile policy_arns = [data.aws_iam_policy.sas_full_rights.arn] } runner_worker_docker_machine_autoscaling_options = var.runners_machine_autoscaling runner_worker_cache = { shared = true bucket_prefix = var.environment expiration_days = 90 } runner_gitlab_registration_config = { type = "instance" # or "group" or "project" # group_id = 1234 # for "group" # project_id = 5678 # for "project" tag_list = "docker" description = "runner default" locked_to_project = "true" run_untagged = "false" maximum_timeout = "3600" } # Refer to https://docs.docker.com/machine/drivers/aws/#options # for 'docker_machine_options' settings with the AWS driver runner_worker_docker_options = var.docker_machine_options tags = local.common_tags } ``` -------------------------------- ### Build Amazon Linux 2023 AMI with Docker Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/packer_images/README.md Use this command to build an Amazon Linux 2023 AMI with Docker installed. The `docker_registry_mirror` argument is optional. ```bash packer build -var 'vpc_id=your_vpc_id' -var 'subnet_id=your_subnet_id' -var 'docker_registry_mirror=docker_registry_url' amz-linux-docker.json ``` -------------------------------- ### Multi-Region GitLab Runner Deployment Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/docs/usage.md Configure for multi-region deployments by using prefixes to avoid name clashes for global AWS resources. This example sets a region-specific prefix for IAM objects and the cache bucket. ```hcl module "runner" { # https://registry.terraform.io/modules/cattle-ops/gitlab-runner/aws/ source = "cattle-ops/gitlab-runner/aws" environment = "multi-region-1" iam_object_prefix = "-gitlab-runner-iam" vpc_id = module.vpc.vpc_id subnet_id = element(module.vpc.private_subnets, 0) runner_gitlab = { url = "https://gitlab.com" preregistered_runner_token_ssm_parameter_name = "my-gitlab-runner-token-ssm-parameter-name" } runner_worker_cache = { bucket_prefix = "" } runner_worker_docker_machine_instance = { subnet_ids = module.vpc.private_subnets } } ``` -------------------------------- ### Terraform Module Usage Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/modules/terminate-agent-hook/README.md Example of how to interact with the terminate-agent-hook module via the parent module's input variables. Ensure `asg_terminate_lifecycle_hook_create` is set to true to enable the hook. ```terraform module "runner" { source = "cattle-ops/gitlab-runner/aws" asg_terminate_lifecycle_hook_create = true ... ``` -------------------------------- ### Basic Cache Module Usage Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/modules/cache/README.md Example of how to include the cache module in your Terraform configuration. This creates a dedicated S3 bucket for caching. ```hcl module "cache" { source = "https://github.com/cattle-ops/terraform-gitlab-runner/tree/move-cache-to-moudle/cache" environment = "cache" } ``` -------------------------------- ### Pipeline before_script for Certificates Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/examples/runner-certificates/README.md Add a `before_script` section to your GitLab CI/CD pipeline configuration to install and trust custom CA certificates within the user image. This approach requires manual addition to each pipeline file. ```yaml default: before_script: # Install certificates into user image - apt-get install -y ca-certificates - cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/ - update-ca-certificates ``` -------------------------------- ### Runner Pre-build Script for Certificates Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/examples/runner-certificates/README.md Configure the `runners_pre_build_script` variable in your Terraform module to automatically execute commands for installing and trusting custom CA certificates in user images. This centralizes the configuration and avoids repetition in pipeline files, assuming a consistent OS across user images. ```terraform module "runner" { # ... runner_worker_gitlab_pipeline = { pre_build_script = </*" } ``` -------------------------------- ### Terraform Error: Invalid count argument with Shared Cache Bucket Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/docs/pitfalls.md When managing the S3 cache bucket yourself, apply the cache resources first using `terraform apply -target=module.cache` before applying the entire module. ```text Error: Invalid count argument on .terraform/modules/gitlab_runner/main.tf line 400, in resource "aws_iam_role_policy_attachment" "docker_machine_cache_instance": count = var.cache_bucket["create"] || length(lookup(var.cache_bucket, "policy", "")) > 0 ? 1 : 0 The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on. ``` -------------------------------- ### Setting GitLab Runner Instance Name Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/docs/pitfalls.md Use the `runner_instance.name` variable to set the EC2 instance name. Setting the `Name` tag in `additional_tags` does not affect the instance name. ```hcl # working runner_instance = { name = "my-gitlab-runner-name" } # not working runner_instance = { additional_tags = { Name = "my-gitlab-runner-name" } } ``` -------------------------------- ### Define Security Group Description for Runner Worker Source: https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/README.md Sets a descriptive string for the security group associated with GitLab Runner worker instances. ```HCL "A security group containing Runner Worker instances" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.