This task automates multi-region deployment and backup automation for file and directory management using Ansible and Terraform on AWS. The automation ensures:
This project helps ensure high availability, disaster recovery, and streamlined configuration management.
provider "aws" {
alias = "primary"
region = "us-east-1"
}
provider "aws" {
alias = "secondary"
region = "us-west-2"
}
resource "aws_vpc" "main_primary" {
provider = aws.primary
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "main_secondary" {
provider = aws.secondary
cidr_block = "10.1.0.0/16"
}
resource "aws_instance" "web_primary" {
provider = aws.primary
ami = "ami-12345678"
instance_type = "t2.micro"
key_name = "aws-key"
tags = {
Name = "WebServerPrimary"
}
}
resource "aws_instance" "web_secondary" {
provider = aws.secondary
ami = "ami-87654321"
instance_type = "t2.micro"
key_name = "aws-key"
tags = {
Name = "WebServerSecondary"
}
}
resource "aws_s3_bucket" "backup_bucket" {
provider = aws.primary
bucket = "nginx-config-backup"
lifecycle {
prevent_destroy = true
}
}
output "primary_instance_ip" {
value = aws_instance.web_primary.public_ip
}
output "secondary_instance_ip" {
value = aws_instance.web_secondary.public_ip
}
terraform init
terraform apply -auto-approve
[primary]
web_primary ansible_host=<PRIMARY_EC2_IP> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/aws-key.pem
[secondary]
web_secondary ansible_host=<SECONDARY_EC2_IP> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/aws-key.pem
---
- name: Manage Directories and Files on Multi-Region AWS EC2
hosts: all
become: yes
tasks:
- name: Create necessary directories
file:
path: "/etc/nginx/custom-config"
state: directory
mode: '0755'
- name: Copy the nginx configuration file
copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: '0644'
notify: Restart nginx
- name: Backup nginx.conf to S3
aws_s3:
bucket: nginx-config-backup
object: "configs/nginx-{{ inventory_hostname }}.conf"
src: /etc/nginx/nginx.conf
mode: put
- name: Ensure nginx service is running
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart nginx
systemd:
name: nginx
state: restarted
ansible-playbook -i hosts file_management.yml
resource "aws_backup_vault" "nginx_backup" {
name = "nginx-backup"
}
resource "aws_backup_plan" "daily_backup" {
name = "daily-backup"
rule {
rule_name = "daily-backup-rule"
target_vault_name = aws_backup_vault.nginx_backup.name
schedule = "cron(0 12 * * ? *)" # Runs daily at noon UTC
lifecycle {
delete_after = 30 # Keep backups for 30 days
}
}
}
resource "aws_backup_selection" "backup_selection" {
name = "nginx-backup-selection"
plan_id = aws_backup_plan.daily_backup.id
iam_role_arn = aws_iam_role.backup_role.arn
resources = [
aws_instance.web_primary.arn,
aws_instance.web_secondary.arn
]
}
terraform apply -auto-approve
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 file_management.yml'
}
}
}
}
This project ensures high availability and disaster recovery by deploying multi-region infrastructure, automating file management with Ansible, and integrating backup automation using AWS S3 and AWS Backup. The CI/CD pipeline ensures seamless deployments and updates. 🚀