DEV Community

Cover image for Replace GitLab Shared Runners With an AWS EC2 Instance
Ali Ogun for AWS Community Builders

Posted on • Originally published at Medium

Replace GitLab Shared Runners With an AWS EC2 Instance

No More GitLab Runner

We love GitLab. We love GitLab CI/CD Pipeline. Out of question. Free GitLab runners are perfect for personal small projects since they are free, available and fully managed. On the other hand it may seem short for the bigger project’s pipelines. And the reason is obvious. Because they are shared with other users.

Ain’t we tired of old and slow GitLab Runners? If the answer is yes, then this article is cut out exactly for you. We will use an AWS EC2 machine for our pipeline to run at. I will stay in free-tier during the article, so no worries.

How to do?

1. Create an AWS account and log in.

2. Create an EC2 machine:

I use t2.micro machine since it will be enough for my projet and it is free-tier eligible. Don’t forget to choose your PEM because we will use it in next step.

t2.micro

I created a keypair beforehand with the name of “protein”

I’ve used Linux/GNU Debian distribution but you can choose any distro you want.

You can choose the computation power of the machine as the size of your CI/CD jobs grow. I’ve used default settings since I was deploying a very basic React app.

3. Connect to EC2 machine:

  • Open an SSH client. Locate your private key file.For instancein my case it is protein.pem. Then run the comment below. But replace the protein.pem with your own PEM file name and also change the PublicIPv4DNS address with your own EC2 machine's ip address:

    $ ssh -i "privatekey.pem" ec2-user@PublicIPv4DNS

4. Take registration token from GitLab:

In the GitLab repository respectively click Settings > CI/CD > Runners. There you will see your registration token. We will use it to connect our pipeline to our EC2 machine.

In this image it is visible the registration token. Copy it fellow

5. Install Gitlab Runner to EC2:

  • Download the binary for your system:

    $ sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

  • Give it permission to execute

    $ sudo chmod +x /usr/local/bin/gitlab-runner

  • Create a GitLab Runner user

    $ sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

  • Install and run as a service

    $ sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner $ sudo gitlab-runner start

6. Register Runner to EC2

$ sudo gitlab-runner register --url [https://gitlab.com/](https://gitlab.com/) --registration-token $REGISTRATION_TOKEN
Enter fullscreen mode Exit fullscreen mode

Don’t forget to replace your registration token in the above script.

7. Activate Your Runner

Now you runners should be visible in Settings > CI/CD > Runners like this:

Click the button shown in the image in order to activate your runners.

7. Deactivate Shared Runners

As default GitLab use shared runners. So, stop shared runners to use your runners to carry out your pipe line executions.

Congratulations! You’re done. Now, you are running your pipeline on your own EC2 machine. As a result, your Settings > CI/CD > Runners screen should looks like this:

Configuration

For advance settingsou can change the behavior of GitLab Runner and of individual registered runners.

To do this, you modify a file called config.toml, which uses the TOML format.

You can find the config.toml file in: /etc/gitlab-runner/

Here as shown:

And the last but not the least; I leave my git repository link here where you can find my config.toml file. Here you can go and take a look at it.

Top comments (2)

Collapse
 
cvanti profile image
Cristian V

and now good luck maintaining them

Collapse
 
ayogun profile image
Ali Ogun

Indeed, lol. Even, I forgot ti mention that perspective of the subject even though it was for fun and experiment environment 😃