1. Answers
  2. Setting up a dedicated GitLab Runner instance on AWS EC2

How do I set up a dedicated GitLab Runner instance on AWS EC2?

In this guide, we will set up a dedicated GitLab Runner instance on AWS EC2 using Pulumi. GitLab Runners are used to run your CI/CD jobs and having a dedicated instance ensures that your builds and deployments have the necessary resources and isolation.

We will create an EC2 instance, configure it with the necessary security groups, and install the GitLab Runner on it.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Define the configuration for the EC2 instance
const size = "t2.micro"; // Instance size
const ami = aws.ec2.getAmi({
    filters: [{ name: "name", values: ["amzn2-ami-hvm-*-x86_64-gp2"] }],
    owners: ["137112412989"], // Amazon
    mostRecent: true,
}).then(ami => ami.id);

// Create a security group allowing SSH and HTTP/HTTPS access
const securityGroup = new aws.ec2.SecurityGroup("gitlab-runner-sg", {
    description: "Allow SSH and HTTP/HTTPS",
    ingress: [
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, // HTTP
        { protocol: "tcp", fromPort: 443, toPort: 443, cidrBlocks: ["0.0.0.0/0"] }, // HTTPS
    ],
    egress: [
        { protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }, // Allow all outbound traffic
    ],
});

// Create an EC2 key pair to access the instance
const keyPair = new aws.ec2.KeyPair("gitlab-runner-keypair", {
    publicKey: "<your-ssh-public-key>",
});

// Create the EC2 instance
const server = new aws.ec2.Instance("gitlab-runner-instance", {
    instanceType: size,
    ami: ami,
    keyName: keyPair.keyName,
    securityGroups: [securityGroup.name],
    userData: `#!/bin/bash
                sudo yum update -y
                sudo yum install -y git curl
                curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
                sudo chmod +x /usr/local/bin/gitlab-runner
                sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
                sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
                sudo gitlab-runner start`,
    tags: {
        Name: "gitlab-runner-instance",
    },
});

// Export the public IP of the server
export const publicIp = server.publicIp;
export const publicDns = server.publicDns;

Key Points

  • We defined the configuration for an EC2 instance using Pulumi.
  • Created a security group to allow SSH, HTTP, and HTTPS access.
  • Created an EC2 key pair for SSH access.
  • Launched an EC2 instance with Amazon Linux 2 AMI.
  • Installed GitLab Runner on the instance using a user data script.

Summary

This guide demonstrated how to set up a dedicated GitLab Runner instance on AWS EC2 using Pulumi. By following these steps, you can ensure your CI/CD jobs run on a dedicated, scalable environment.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up