Multi-Region High Availability Deployment using Ansible
This project will guide you through setting up a highly available, multi-region application using AWS services and
Ansible for automation. The goal is to deploy an application in multiple AWS regions and implement failover mechanisms using
Route 53 health checks and DNS failover.
Task Overview
- Deploy a highly available web application across two AWS regions.
- Automate infrastructure provisioning using Terraform.
- Automate application deployment using Ansible.
- Implement Route 53 DNS failover with health checks.
- Use RDS Multi-AZ for database high availability.
- Configure CloudWatch monitoring and alerts.
Technology Stack
AWS Services:
- EC2 (Application Servers)
- S3 (Static Content)
- RDS Multi-AZ (Database)
- Route 53 (DNS Failover)
- ALB (Load Balancer)
- CloudWatch (Monitoring)
- Auto Scaling Groups
Automation Tools:
- Terraform (Infrastructure as Code)
- Ansible (Configuration Management & Deployment)
Application Stack:
- Nginx (Reverse Proxy)
- Tomcat (Java Web Application)
- MySQL RDS (Relational Database)
Monitoring & Logging:
- CloudWatch Metrics & Alarms
- ELK Stack (Optional)
Project Architecture
Primary & Secondary Region Deployment
- Primary Region (us-east-1): Deployed with EC2, RDS (Primary), ALB, Auto Scaling.
- Secondary Region (us-west-2): Deployed as a standby region with EC2, RDS (Read Replica), ALB, Auto Scaling.
- Route 53 Failover: Uses health checks to detect failure in the primary region and automatically routes traffic to the secondary region.
Step-by-Step Implementation
1. Set Up AWS Infrastructure using Terraform
Terraform Tasks:
- Define VPC, Subnets, Internet Gateway, and Route Tables.
- Deploy EC2 instances in both regions.
- Set up Auto Scaling Groups and ALB.
- Deploy an RDS database with Multi-AZ and a Read Replica.
- Configure Route 53 for failover.
Terraform Code Structure
├── terraform/
│ ├── main.tf # Define AWS provider, backend, and modules
│ ├── vpc.tf # Create VPC, Subnets, and Routes
│ ├── ec2.tf # Deploy EC2 instances
│ ├── rds.tf # Configure Multi-AZ RDS
│ ├── alb.tf # Deploy ALB and Auto Scaling Groups
│ ├── route53.tf # Configure Route 53 DNS failover
│ ├── variables.tf # Define variables
│ ├── outputs.tf # Define output variables
│ ├── terraform.tfvars # Define values for variables
Commands to Deploy with Terraform:
cd terraform
terraform init
terraform plan
terraform apply -auto-approve
2. Configure Application Deployment using Ansible
Once Terraform provisions the infrastructure, Ansible will configure and deploy the application.
Ansible Tasks:
- Install Nginx (Reverse Proxy) & Tomcat (App Server).
- Deploy the application code.
- Configure MySQL database connection.
- Automate deployment with Ansible playbooks.
Ansible Directory Structure
├── ansible/
│ ├── inventory.ini # Inventory with EC2 instances
│ ├── roles/
│ │ ├── nginx/
│ │ │ ├── tasks/main.yml # Install and configure Nginx
│ │ ├── tomcat/
│ │ │ ├── tasks/main.yml # Deploy Tomcat and App
│ │ ├── mysql/
│ │ │ ├── tasks/main.yml # Configure MySQL RDS
│ ├── site.yml # Main playbook
Example Ansible Playbook (site.yml)
- name: Configure and deploy application
hosts: all
become: yes
roles:
- nginx
- tomcat
- mysql
Run Ansible Playbook
cd ansible
ansible-playbook -i inventory.ini site.yml
3. Configure Route 53 Failover with Health Checks
- Route 53 Primary Record: Points to the primary region ALB.
- Route 53 Secondary Record: Points to the secondary region ALB.
- Health Checks: Monitor the primary region.
- If the primary region fails, Route 53 automatically redirects traffic to the secondary region.
Route 53 Configuration in Terraform
resource "aws_route53_record" "primary" {
zone_id = aws_route53_zone.main.zone_id
name = "app.example.com"
type = "A"
set_identifier = "primary"
failover_routing_policy {
type = "PRIMARY"
}
alias {
name = aws_lb.primary_lb.dns_name
zone_id = aws_lb.primary_lb.zone_id
evaluate_target_health = true
}
health_check_id = aws_route53_health_check.primary.id
}
resource "aws_route53_health_check" "primary" {
type = "HTTPS"
resource_path = "/health"
fqdn = aws_lb.primary_lb.dns_name
request_interval = 30
failure_threshold = 3
}
4. Monitor and Automate Failover
- CloudWatch Alarms: Notify when the primary region is down.
- Auto Scaling: Automatically replace failed instances.
- Failover Testing: Simulate primary region failure and verify traffic routing to the secondary region.
End-to-End Workflow
- Terraform provisions AWS infrastructure.
- Ansible installs and configures the application.
- Route 53 health checks ensure automatic failover.
- CloudWatch & Auto Scaling maintain high availability.
Expected Outcomes
- ✅ Application available in two AWS regions
- ✅ Automatic failover in case of failure
- ✅ Automated infrastructure provisioning
- ✅ Continuous monitoring with CloudWatch
Next Steps
- Extend the setup to use CDN (CloudFront) for static content.
- Implement Infrastructure as Code (IaC) with GitHub Actions.
- Deploy a multi-cloud solution with Azure/AWS failover.
Conclusion
This project ensures high availability and disaster recovery using AWS services, Terraform, and Ansible.
With automated failover via Route 53, your application remains online even if a region fails.