Disaster recovery (DR) is crucial for ensuring business continuity in case of failures, cyberattacks, or natural disasters. This project aims to automate backup and recovery of critical infrastructure and databases using Ansible, AWS, and Terraform. The solution will periodically back up infrastructure and databases and provide an automated mechanism for restoring services in case of failure.
Provision critical AWS resources:
Create Ansible playbooks to:
resource "aws_s3_bucket" "backup_bucket" {
bucket = "my-disaster-recovery-bucket"
lifecycle_rule {
id = "auto-expire"
enabled = true
expiration {
days = 30
}
}
}
resource "aws_rds_instance" "database" {
engine = "mysql"
instance_class = "db.t3.micro"
allocated_storage = 20
identifier = "dr-db-instance"
backup_retention_period = 7
}
resource "aws_instance" "app_server" {
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "App-Server"
}
}
---
- name: Backup EC2 and RDS
hosts: localhost
tasks:
- name: Take EC2 snapshot
community.aws.ec2_snapshot:
instance_id: "{{ ec2_instance_id }}"
region: "{{ aws_region }}"
wait: yes
register: ec2_snapshot
- name: Backup RDS database
community.aws.rds_snapshot:
db_instance_identifier: "{{ rds_instance }}"
db_snapshot_identifier: "rds-backup-{{ ansible_date_time.epoch }}"
wait: yes
register: rds_backup
- name: Copy application data to S3
aws_s3:
bucket: "my-disaster-recovery-bucket"
object: "/backups/app-data-{{ ansible_date_time.epoch }}.tar.gz"
src: "/var/www/html/"
mode: put
---
- name: Restore EC2 and RDS
hosts: localhost
tasks:
- name: Restore EC2 from snapshot
community.aws.ec2_snapshot_info:
snapshot_ids: "{{ latest_snapshot_id }}"
register: ec2_snapshot_info
- name: Create new EC2 from latest snapshot
community.aws.ec2_instance:
name: "Recovered-App-Server"
region: "{{ aws_region }}"
image_id: "{{ ec2_snapshot_info.snapshots[0].image_id }}"
instance_type: "t2.micro"
wait: yes
- name: Restore RDS from latest snapshot
community.aws.rds_instance:
identifier: "recovered-db-instance"
snapshot_identifier: "{{ latest_rds_snapshot_id }}"
instance_class: "db.t3.micro"
wait: yes
1.Configure CloudWatch Alarms:
Set up Ansible handlers:
---
- name: Monitor EC2 and Trigger Recovery
hosts: localhost
tasks:
- name: Check if EC2 instance is running
shell: aws ec2 describe-instance-status --instance-id "{{ ec2_instance_id }}"
register: ec2_status
- name: Trigger recovery if EC2 is down
command: ansible-playbook restore.yml
when: "'running' not in ec2_status.stdout"