Deploying Jenkins Controller to AWS

Deploying Jenkins Controller to AWS

4 min read
DevOps CI/CD
Table of Contents

Deploying Jenkins Controller on AWS Using Docker-Compose with SSL and Route 53

Introduction

Jenkins is an open-source automation server that enables developers to build, test, and deploy their software. Deploying a Jenkins controller using Docker Compose simplifies the setup process, making it more manageable and consistent. This guide will walk you through deploying a Jenkins controller with Docker Compose, setting up SSL certificates using Certbot, and configuring a Route 53 hosted zone for DNS management.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  1. Docker and Docker Compose installed on your machine.
  2. An AWS account with Route 53 permissions.
  3. A registered domain name.
  4. Access to port 80 and 443 on your server for HTTP and HTTPS traffic.

Step 1: Setting Up Docker Compose

First, create a directory for your Jenkins deployment. Within this directory, create a docker-compose.yml file. This file will define the Jenkins controller and Nginx as a reverse proxy for handling SSL termination.

version: '3'

services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    ports:
      - "8080:8080"
    volumes:
      - jenkins_home:/var/jenkins_home
    networks:
      - jenkins_network

  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - certs:/etc/letsencrypt
    depends_on:
      - jenkins
    networks:
      - jenkins_network

volumes:
  jenkins_home:
  certs:

networks:
  jenkins_network:

Next, create an nginx.conf file in the same directory to configure Nginx as a reverse proxy.

events { }

http {
    server {
        listen 80;
        server_name your_domain.com;

        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name your_domain.com;

        ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;

        location / {
            proxy_pass http://jenkins:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

Replace your_domain.com with your actual domain name.

Step 2: Obtaining SSL Certificates with Certbot

Certbot is a free, automated tool that helps you obtain and renew SSL certificates from Let’s Encrypt. To use Certbot, add the following to your docker-compose.yml file:

certbot:
    image: certbot/certbot
    container_name: certbot
    volumes:
      - certs:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
    entrypoint: /bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'

Now, run the following command to start the services and obtain the certificates:

docker-compose up -d

To obtain the initial certificates, run:

docker-compose run --rm certbot certonly --webroot --webroot-path=/var/www/certbot -d your_domain.com

Certbot will now generate the SSL certificates and store them in the certs volume. Nginx will automatically pick them up and start serving your Jenkins instance over HTTPS.

Step 3: Configuring Route 53

To manage your DNS records using AWS Route 53, follow these steps:

  1. Create a Hosted Zone:

    • Log in to the AWS Management Console.
    • Navigate to Route 53 and click on “Hosted Zones.”
    • Click “Create Hosted Zone” and enter your domain name.
    • Note the NS (Name Server) records provided.
  2. Update Your Domain Registrar:

    • Log in to your domain registrar’s website.
    • Update the NS records to point to the ones provided by Route 53.
  3. Create an A Record for Jenkins:

    • Go back to Route 53 in the AWS Management Console.
    • Click on your hosted zone and then “Create Record.”
    • Choose “A – IPv4 address” as the record type.
    • Enter the subdomain (e.g., jenkins.your_domain.com) and the public IP address of your server.
    • Save the record.

Conclusion

By following these steps, you’ve successfully deployed a Jenkins controller using Docker Compose, secured it with SSL certificates from Let’s Encrypt using Certbot, and configured DNS management with AWS Route 53. This setup ensures a reliable, secure, and scalable Jenkins environment for your CI/CD pipelines.

Remember to regularly check your SSL certificates and renew them as needed, although Certbot automates this process. With this deployment, your Jenkins instance is now accessible over HTTPS, providing a secure interface for your development operations.