cdk-gitlab-runner

2.4.18 · active · verified Thu Apr 16

cdk-gitlab-runner is a Python library that leverages AWS CDK to provision and manage GitLab CI/CD runners within your AWS environment. It supports various configurations, including Fargate and EC2 instances, enabling users to quickly deploy self-hosted runners to execute GitLab pipeline jobs. The library is actively maintained, with frequent updates to support new AWS CDK and GitLab Runner features, and the current version is 2.4.18.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart deploys a GitLab Container Runner on an EC2 instance within your default VPC using AWS CDK. It retrieves the GitLab Runner token from an environment variable (recommended for security) and configures the runner with a specific GitLab Runner version, EC2 instance type, and Docker image. Remember to set the `GITLAB_RUNNER_TOKEN` environment variable or replace the placeholder.

import os
from aws_cdk import App, Stack
from cdk_gitlab_runner import GitlabContainerRunner
from aws_cdk.aws_ec2 import Vpc


class MyGitlabRunnerStack(Stack):
    def __init__(self, scope: App, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Retrieve an existing VPC by ID or create a new one
        # For simplicity, a default VPC lookup is used. In production, provide a specific vpc_id.
        # Ensure your AWS account is bootstrapped and CDK context is up-to-date for VPC lookup.
        vpc = Vpc.from_lookup(self, "VPC", is_default=True)

        # Get GitLab Token from environment variable (secure practice)
        gitlab_token = os.environ.get('GITLAB_RUNNER_TOKEN', 'glrt-YOUR_GITLAB_TOKEN_HERE')

        if gitlab_token == 'glrt-YOUR_GITLAB_TOKEN_HERE':
            print("WARNING: Replace 'glrt-YOUR_GITLAB_TOKEN_HERE' with a real GitLab Runner Token or set GITLAB_RUNNER_TOKEN env var.")

        # Create a GitLab Container Runner
        runner = GitlabContainerRunner(
            self, 
            'MyGitlabRunner',
            vpc=vpc,
            gitlab_token=gitlab_token,
            gitlab_runner_version='16.9.1', # Specify a compatible GitLab Runner version
            ec2_type='t3.micro', # Instance type for the runner
            docker_image_name='python:3.9-slim-buster', # Docker image for jobs
            runner_tags=['aws', 'cdk', 'python'], # Tags for matching jobs
        )

app = App()
MyGitlabRunnerStack(app, "MyGitlabRunnerStack")
app.synth()

view raw JSON →