CI/CD at Scale: Deploying Jenkins on AWS with Terraform Automation


In today’s fast-paced DevOps landscape, Continuous Integration and Continuous Deployment (CI/CD) are essential for delivering quality software efficiently and reliably. Jenkins, one of the most popular open-source CI/CD tools, becomes even more powerful when combined with AWS for scalability and Terraform for automation. This guide walks you through deploying a scalable Jenkins architecture on AWS using Terraform, enabling automation, reproducibility, and efficient infrastructure management.


 Why Jenkins + AWS + Terraform?

Jenkins for CI/CD

Jenkins orchestrates the automation of software builds, tests, and deployments, supporting hundreds of plugins to integrate with any development or deployment technology.

AWS for Infrastructure

AWS provides elastic and scalable infrastructure like EC2, S3, and IAM, which ensures Jenkins can scale horizontally and integrate seamlessly with cloud-native tools.

Terraform for Automation

Terraform allows you to define infrastructure as code (IaC), enabling version control, repeatability, and automation across environments.


 Architecture Overview

  • Jenkins Master on EC2: Runs the Jenkins controller

  • Jenkins Agents: Auto-scaled EC2 instances

  • S3: Stores Jenkins backups and artifacts

  • IAM Roles: Securely access AWS services

  • Security Groups: Manage access control

  • Terraform: Automates provisioning of all the above resources


Step-by-Step Setup

Step 1: Create Terraform Project

Organize your code with modules for:

  • Network (VPC, subnets)

  • Compute (EC2, Auto Scaling Groups)

  • Storage (S3 buckets)

  • Security (IAM roles, security groups)

Step 2: Define Terraform Resources

Sample EC2 Instance for Jenkins Master


resource "aws_instance" "jenkins_master" {

  ami                    = "ami-0abcdef1234567890"

  instance_type          = "t2.medium"

  key_name               = var.key_name

  vpc_security_group_ids = [aws_security_group.jenkins_sg.id]

  user_data              = file("jenkins-install.sh")

  tags = {

    Name = "JenkinsMaster"

  }

}


IAM Role for Jenkins EC2


resource "aws_iam_role" "jenkins_role" {

  name = "jenkins-role"

  assume_role_policy = jsonencode({

    Version = "2012-10-17",

    Statement = [{

      Action = "sts:AssumeRole",

      Effect = "Allow",

      Principal = {

        Service = "ec2.amazonaws.com"

      }

    }]

  })

}


Step 3: Install Jenkins via User Data Script

Your jenkins-install.sh should install Java, Jenkins, and configure Jenkins as a service. Also, it should set the appropriate ports and install the required plugins.

Step 4: Output Jenkins URL

Use Terraform outputs to capture the public DNS or IP:


output "jenkins_url" {

  value = "http://${aws_instance.jenkins_master.public_dns}:8080"

}



Scaling Jenkins with ASG

Configure an Auto Scaling Group and Launch Template to scale Jenkins agents based on job queue size dynamically. Jenkins can automatically connect with these agents using the EC2 plugin configuration.


Securing Your Jenkins Environment

  • Use HTTPS with a reverse proxy (Nginx or ALB).

  • Store credentials in AWS Secrets Manager.

  • Enable AWS CloudWatch for logging and monitoring.


CI/CD Pipeline Example

Once Jenkins is deployed:

  1. Connect to your GitHub/GitLab repository.

  2. Define a Jenkinsfile for your pipeline:


pipeline {

    agent any

    stages {

        stage('Build') {

            steps {

                sh './build.sh'

            }

        }

        stage('Test') {

            steps {

                sh './run-tests.sh'

            }

        }

        stage('Deploy') {

            steps {

                sh './deploy.sh'

            }

        }

    }

}


  1. Use Jenkins plugins to trigger pipelines on code pushes or pull requests.


Benefits of This Setup

  • Scalability: Add agents on demand with minimal cost.

  • Reproducibility: Rebuild entire infra using Terraform.

  • Security: Use AWS IAM best practices and secrets management.

  • Portability: Manage Jenkins as code using declarative scripts.


Conclusion

Combining Jenkins with AWS and Terraform transforms your CI/CD pipeline into a robust, automated, scalable system. Whether you're a startup scaling fast or an enterprise modernizing workflows, this integration sets the foundation for continuous innovation.


Comments

YouTube Channel

Follow us on X