Replace GitLab Shared Runners With an AWS EC2 Instance
Tags: aws, devops, ec2, gitlab
Published: 2023-09-09 12:46:48

### 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.

![](https://cdn-images-1.medium.com/max/2000/1*5_RENgafvH9j6OPh_CSnNQ.png)

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](https://portal.aws.amazon.com/billing/signup#/start/email) 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](https://cdn-images-1.medium.com/max/3184/1*GLCaYRoYVeVbssM_meUyVw.png)

![I created a keypair beforehand with the name of “protein”](https://cdn-images-1.medium.com/max/3216/1*ezEqnAW1PUyvzF-2T70S0Q.png)
>  I’ve used Linux/GNU Debian distribution but you can choose any distro you want.

![](https://cdn-images-1.medium.com/max/2992/1*suugGNalg4mSCKEqwRH6pg.png)

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](https://cdn-images-1.medium.com/max/5760/1*etbUkk3--56tsluLQ8V4Hw.png)

**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](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

**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:

![](https://cdn-images-1.medium.com/max/5760/1*0ZWYhE4jntMM2DOrfWq8Vg.png)

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.

![](https://cdn-images-1.medium.com/max/5760/1*CHBKCYD-8Atmtv7LzU0GFA.png)

**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:

![](https://cdn-images-1.medium.com/max/5760/1*kBNFh5z1_cZPNcyuMDMe-A.png)

## 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:

![](https://cdn-images-1.medium.com/max/2588/1*XqV6LMTfbl41E0193Edzvw.png)

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.](https://github.com/byaliego/Final_Project-DevOps_Bootcamp/blob/main/bonus_part/gitlab-cicd-runner/config.toml)