Automated Service Management Using Ansible on AWS

Task Overview

This Task automates the installation, management, and monitoring of essential services (nginx, apache2, sshd) on AWS EC2 instances using Ansible. The automation ensures:

1. Tools & Technologies

2. Task Architecture

  1. Terraform provisions AWS infrastructure:
  2. Ansible manages EC2 instances:
  3. CI/CD pipeline automates deployments:

3. Infrastructure Setup using Terraform

Step 1: Create Terraform Configuration (main.tf)

provider "aws" {
  region = "us-east-1"
}
e
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
}

resource "aws_security_group" "allow_ssh_http" {
  vpc_id = aws_vpc.main.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.public.id
  vpc_security_group_ids = [aws_security_group.allow_ssh_http.id]
  key_name      = "aws-key"
  
  tags = {
    Name = "WebServer"
  }
}
    

4. Configuring Ansible for EC2 Instances

Step 2: Define Ansible Inventory (hosts)

[webservers]
webserver ansible_host=<EC2_PUBLIC_IP> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/aws-key.pem

Step 3: Create Ansible Playbook (manage_services.yml)

---
- name: Manage Services on AWS EC2
  hosts: webservers
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name: "{{ item }}"
        state: present
        update_cache: yes
      loop:
        - nginx
        - apache2
        - openssh-server

    - name: Enable services to start on boot
      systemd:
        name: "{{ item }}"
        enabled: yes
      loop:
        - nginx
        - apache2
        - ssh

5. CI/CD Pipeline Integration

Step 4: Jenkins Pipeline Configuration

pipeline {
    agent any
    stages {
        stage('Clone Repo') {
            steps {
                git 'https://github.com/your-repo.git'
            }
        }
        stage('Deploy Infrastructure') {
            steps {
                sh 'terraform init && terraform apply -auto-approve'
            }
        }
        stage('Configure EC2 with Ansible') {
            steps {
                sh 'ansible-playbook -i hosts manage_services.yml'
            }
        }
    }
}

7. Project Deliverables

Conclusion

This project provides a scalable, automated service management system using AWS, Terraform, Ansible, and Jenkins. 🚀