How To Install Multiple Gitlab Runners On a Single AWS EC2 machine
Tags: gitlab, cicd, aws, ec2
Published: 2022-12-05 17:19:52

**In this quick tutorial, I am going to show you how you can install multiple Gitlab Runners on a single AWS EC2 machine.**

## Step 1
**Provision a EC2 server**

Login to you AWS console--> EC2 --> Hit launch instances


![Launch Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkgtfu9ilr5wykev14pr.png)
Give it a name such as Gitlab-Runner and select a suitable Linux Image. In this demo, I have selected an Ubuntu image


![Create the EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iltjfeianlz76q0lj3tx.png)

Then scroll down and select the key-pair that you would want to use for this instance or you can create a new key-pair. Once the key-pair is selected, you can create a new security group with the wizard and open access to port 22(SSH) from your local machine or a CIDR range and for your application server so that gitlab runner can ssh into your application server for code deployment. You can optionally use an existing security group as well.


![Create a security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p49mxy8shku7e3oxskhw.png)

Scroll down to the advance details section and keep everything as default except the user data section. Add the below user data script and hit Launch instance.

```
#!/bin/bash
sudo apt-get update -y
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

sudo apt-get install gitlab-runner -y

#install docker 
curl -fsSL https://get.docker.com -o get-docker.sh 
sudo bash get-docker.sh

cd /var/run/ 
sudo chmod 777 docker.pid docker.sock

```

![User data script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wutddjsollyh97g8bqhf.png)

## Step 2

**Register the Runner**

Once server is running, login to the server using EC2 instance connect or ssh client such as Putty and enter the command

` gitlab-runner register `

then enter your gitlab running when prompted


![Gitlab Runner Register](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czmguu2aaxrtq728seni.png)

Go to your Gitlab UI --> Settings --> CICD and Expand the section in front of Runners


![Gitlab Runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6oputhzu3vex82cq33w.png)

Copy the registration token from here and paste the same in the terminal prompt

![Copy the token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiwgicrjpvwg9dzd0g7w.png)

Enter the details as below to register the runner


![Register Runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujus7p9ognouyzkhg6v1.png)

## Step 3

**Start the Runner**

- Start the runner using the below command:
`sudo gitlab-runner start`

once it is started, hit refresh on the Gitlab page and you would see the runner as active


![Runner as active](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5nzko0o5l9situcp0k3.png)

Give yourself a pat on the back! you were able to perform the task successfully!

## Step4
**Register second runner(Optional)**
Now , if you would like to register multiple runners on the same ec2 server, using can do so by following the same method. Give a different name and tag to the runner as follows


![Second runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a8rnd1aevndziex49zi.png)

Now, refresh the page to see the new runner as well


![Runner final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1x403yu2jl1wl1ma5r8.png)

You can use the runners based on the tags or the names.

That's all folks. If you have enjoyed the blog and learned something new today. Feel free to hit the like button and follow me for more such content.

I have also published a full end to end tutorial for Gitlab CI CD Pipeline, feel free to check that out using the below link


{% embed https://youtu.be/uYaMRn2meJQ %}