This Task automates the installation, management, and monitoring of essential services (nginx, apache2, sshd) on AWS EC2 instances using Ansible. The automation ensures:
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"
}
}
[webservers]
webserver ansible_host=<EC2_PUBLIC_IP> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/aws-key.pem
---
- 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
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'
}
}
}
}
This project provides a scalable, automated service management system using AWS, Terraform, Ansible, and Jenkins. 🚀