# mockupAWS Infrastructure

This directory contains all infrastructure-as-code, monitoring, and deployment configurations for mockupAWS production environments.

## Structure

```
infrastructure/
├── terraform/           # Terraform configurations
│   ├── modules/        # Reusable Terraform modules
│   │   ├── vpc/       # VPC networking
│   │   ├── rds/       # PostgreSQL database
│   │   ├── elasticache/ # Redis cluster
│   │   ├── ecs/       # Container orchestration
│   │   ├── alb/       # Load balancer
│   │   ├── cloudfront/# CDN
│   │   └── s3/        # Storage & backups
│   └── environments/   # Environment-specific configs
│       ├── dev/
│       ├── staging/
│       └── prod/      # Production infrastructure
├── ansible/           # Server configuration
│   ├── playbooks/
│   ├── roles/
│   └── inventory/
├── monitoring/        # Monitoring & alerting
│   ├── prometheus/
│   ├── grafana/
│   └── alerts/
└── k8s/              # Kubernetes manifests (optional)
```

## Quick Start

### 1. Deploy Production Infrastructure (AWS)

```bash
# Navigate to production environment
cd terraform/environments/prod

# Create terraform.tfvars
cat > terraform.tfvars <<EOF
environment = "production"
region = "us-east-1"
domain_name = "mockupaws.com"
certificate_arn = "arn:aws:acm:..."
ecr_repository_url = "123456789012.dkr.ecr.us-east-1.amazonaws.com/mockupaws"
alert_email = "ops@mockupaws.com"
EOF

# Initialize and deploy
terraform init
terraform plan
terraform apply
```

### 2. Configure Server (Docker Compose)

```bash
# Run Ansible playbook
ansible-playbook -i ansible/inventory/production ansible/playbooks/setup-server.yml
```

### 3. Deploy Monitoring Stack

```bash
# Start monitoring services
docker-compose -f docker-compose.monitoring.yml up -d

# Access:
# - Prometheus: http://localhost:9090
# - Grafana: http://localhost:3000 (admin/admin)
# - Alertmanager: http://localhost:9093
```

## Terraform Modules

### VPC Module

Creates a production-ready VPC with:
- Public, private, and database subnets
- NAT Gateways
- VPC Flow Logs
- Network ACLs

### RDS Module

Creates PostgreSQL database with:
- Multi-AZ deployment
- Automated backups
- Encryption at rest
- Performance Insights
- Enhanced monitoring

### ECS Module

Creates container orchestration with:
- Fargate launch type
- Auto-scaling policies
- Service discovery
- Circuit breaker deployment

### CloudFront Module

Creates CDN with:
- SSL/TLS termination
- WAF integration
- Origin access identity
- Cache behaviors

## Monitoring

### Prometheus Metrics

- Application metrics (latency, errors, throughput)
- Infrastructure metrics (CPU, memory, disk)
- Database metrics (connections, query performance)
- Redis metrics (memory, hit rate, connections)

### Grafana Dashboards

1. **Overview Dashboard** - Application health and performance
2. **Database Dashboard** - PostgreSQL metrics
3. **Infrastructure Dashboard** - Server and container metrics
4. **Business Dashboard** - User activity and scenarios

### Alerting Rules

- **Critical:** Service down, high error rate, disk full
- **Warning:** High latency, memory usage, slow queries
- **Info:** Low traffic, deployment notifications

## Deployment

### CI/CD Pipeline

GitHub Actions workflows:
- `ci.yml` - Build, test, security scans
- `deploy-production.yml` - Deploy to production

### Deployment Methods

1. **ECS Blue-Green** - Zero-downtime deployment
2. **Docker Compose** - Single server deployment
3. **Kubernetes** - Enterprise multi-region deployment

## Security

### Network Security

- Security groups with minimal access
- Network ACLs
- VPC Flow Logs
- AWS WAF rules

### Data Security

- Encryption at rest (KMS)
- TLS 1.3 in transit
- Secrets management (AWS Secrets Manager)
- Regular security scans

### Access Control

- IAM roles with least privilege
- MFA enforcement
- Audit logging
- Regular access reviews

## Cost Optimization

### Reserved Capacity

- RDS Reserved Instances: ~40% savings
- ElastiCache Reserved Nodes: ~30% savings
- Savings Plans for compute: ~20% savings

### Right-sizing

- Use Fargate Spot for non-critical workloads
- Enable auto-scaling to handle traffic spikes
- Archive old data to Glacier

### Monitoring Costs

- Set up AWS Budgets
- Enable Cost Explorer
- Tag all resources
- Review monthly cost reports

## Troubleshooting

### Common Issues

**Terraform State Lock**
```bash
# Force unlock (use with caution)
terraform force-unlock <LOCK_ID>
```

**ECS Deployment Failure**
```bash
# Check service events
aws ecs describe-services --cluster mockupaws-production --services backend

# Check task logs
aws logs tail /ecs/mockupaws-production --follow
```

**Database Connection Issues**
```bash
# Check RDS status
aws rds describe-db-instances --db-instance-identifier mockupaws-production

# Test connection
pg_isready -h <endpoint> -p 5432 -U mockupaws_admin
```

## Maintenance

### Regular Tasks

- **Daily:** Review alerts, check backups
- **Weekly:** Review performance metrics, update dependencies
- **Monthly:** Security patches, cost review
- **Quarterly:** Disaster recovery test, access review

### Updates

```bash
# Update Terraform providers
terraform init -upgrade

# Update Ansible roles
ansible-galaxy install -r requirements.yml --force

# Update Docker images
docker-compose -f docker-compose.monitoring.yml pull
docker-compose -f docker-compose.monitoring.yml up -d
```

## Support

For infrastructure support:
- **Documentation:** https://docs.mockupaws.com/infrastructure
- **Issues:** Create ticket in GitHub
- **Emergency:** +1-555-DEVOPS (24/7)

## License

This infrastructure code is part of mockupAWS and follows the same license terms.
