DEV Community

Cover image for Terraform, Ansible and Jenkins
muhirwaJD
muhirwaJD

Posted on

Terraform, Ansible and Jenkins

AutoJenkins: CI/CD Deployment with Terraform & Ansible on AWS


🌍 Website / Demo

http://YOUR_EC2_PUBLIC_IP:8080 (replace with real IP)


πŸ“Œ Short Description

We automated the full deployment of Jenkins on AWS using Infrastructure as Code (IaC) with Terraform and Ansible. In just one command, an EC2 instance is created, secured, configured with SSH, and provisioned with a fully working Jenkins CI server β€” without ever logging into the instance.


πŸ’‘ Inspiration

Manually launching cloud infrastructure and setting up CI/CD tools is time-consuming and error-prone. We wanted a fully automated, repeatable solution that reflects real-world DevOps practices, using industry-standard tools like Terraform and Ansible.


πŸ”§ What It Does

  • Terraform provisions:
    • A new AWS EC2 Ubuntu instance
    • A security group with SSH & Jenkins ports open
    • An SSH key for secure access
  • Ansible configures the instance by:
    • Installing OpenJDK 17
    • Adding the Jenkins APT repo & GPG key
    • Installing Jenkins
    • Ensuring the Jenkins service is enabled and running

🧠 How We Built It

  • Used Terraform to define infrastructure as code in main.tf
  • Used Ansible to define the configuration in playbook.yaml
  • Automatically passed the EC2 public IP from Terraform to Ansible
  • Ran everything from a single terminal with:

    terraform apply -auto-approve
    ansible-playbook -i inventory playbook.yaml
    
  • Debugged service failures by parsing logs with Ansible (not SSH)


🧩 Challenges We Ran Into

  • Jenkins failed to start with Java 11. We read logs via journalctl using Ansible and discovered Jenkins now requires Java 17+
  • EC2 SSH keys had to be managed properly to avoid permission errors
  • Jenkins port (8080) had to be opened in the security group

βœ… Accomplishments We're Proud Of

  • Zero SSH login required throughout the project
  • Clean separation of infrastructure (Terraform) and config (Ansible)
  • Used proper logging and error handling via Ansible
  • Debugged systemd failures using journalctl inside playbooks

πŸš€ What’s Next

  • Add DNS and HTTPS (via Route 53 + Let’s Encrypt)
  • Integrate GitHub Webhooks to trigger Jenkins jobs automatically
  • Use S3 and CloudWatch for backups and monitoring

πŸ“š Built With

  • ☁️ AWS EC2
  • 🧱 Terraform
  • βš™οΈ Ansible
  • 🐧 Ubuntu 22.04
  • πŸ’» Jenkins

πŸ‘₯ Team

  • Muhirwa β€” DevOps Engineer & Builder

GitHub - muhirwaJD/Terraform-and-Ansible


Top comments (0)