Infraestructura
Principios
- Local-first: Todo el stack corre en Docker Compose para desarrollo local
- AWS-native en produccion: Servicios gestionados donde sea posible (menos operacion)
- Costo-eficiente: Serverless y auto-scaling para pagar solo por lo que se usa
- FTR-compliant: Diseñado para certificacion AWS Partner desde el inicio
- Multi-region ready: Arquitectura preparada para despliegue en multiples regiones
Entorno Local (Docker Compose)
docker-compose.infra.yml
Infraestructura compartida que todos los servicios necesitan:
yaml
services:
postgres:
image: postgres:16-alpine
ports:
- "5432:5432"
environment:
POSTGRES_USER: imagy_admin
POSTGRES_PASSWORD: localdev
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U imagy_admin"]
interval: 5s
timeout: 3s
retries: 5
valkey:
image: valkey/valkey:8-alpine
ports:
- "6379:6379"
command: >
valkey-server
--save 60 1000
--appendonly yes
--maxmemory 256mb
--maxmemory-policy allkeys-lru
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
rabbitmq:
image: rabbitmq:3-management-alpine
ports:
- "5672:5672"
- "15672:15672"
environment:
RABBITMQ_DEFAULT_USER: imagy
RABBITMQ_DEFAULT_PASS: localdev
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "check_running"]
interval: 10s
timeout: 5s
retries: 5
keycloak:
image: quay.io/keycloak/keycloak:24.0
ports:
- "8080:8080"
command: start-dev
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
KC_DB_USERNAME: imagy_admin
KC_DB_PASSWORD: localdev
depends_on:
postgres:
condition: service_healthy
volumes:
pgdata:Levantar el entorno
bash
# Infraestructura
docker compose -f docker-compose.infra.yml up -d
# Cada servicio individualmente
dotnet run --project src/Imagy.Flow.Api
# O todos los servicios con compose
docker compose up -dEntorno de Produccion (AWS)
Servicios AWS
Compute
| Servicio | Uso | Justificacion |
|---|---|---|
| ECS Fargate | Todos los microservicios .NET | Serverless containers, sin EC2 que gestionar, auto-scaling |
| ECS Service Connect | Comunicacion interna entre servicios | Service mesh nativo, sin YARP interno |
| CloudFront | Assets estaticos del frontend | CDN global, cache, HTTPS |
Datos
| Servicio | Uso | Justificacion |
|---|---|---|
| Aurora PostgreSQL Serverless v2 | Base de datos principal | Auto-scaling, Multi-AZ, read replicas, pago por uso |
| ElastiCache Valkey Serverless | Cache y sesiones | Compatible Redis, auto-scaling, sin nodos que gestionar |
| Amazon MQ for RabbitMQ | Mensajeria entre servicios | Managed RabbitMQ, Multi-AZ, sin operacion |
| S3 | Documentos, imagenes, evidencias | Durabilidad 11 9s, encryption SSE-KMS, lifecycle policies |
Seguridad
| Servicio | Uso |
|---|---|
| AWS WAF | Proteccion OWASP, rate limiting, geo-blocking |
| AWS KMS | Customer Managed Keys para encryption |
| AWS Secrets Manager | Credenciales con rotacion automatica |
| AWS GuardDuty | Threat detection |
| AWS Security Hub | Compliance dashboard |
| AWS CloudTrail | Audit de API calls AWS |
Observabilidad
| Servicio | Uso |
|---|---|
| CloudWatch Logs | Centralized logging (Serilog sink) |
| CloudWatch Metrics | Metricas custom por servicio |
| CloudWatch Alarms | Alertas (latencia, errores, CPU) |
| AWS X-Ray | Distributed tracing entre servicios |
| CloudWatch Dashboards | Visualizacion operacional |
Networking
| Servicio | Uso |
|---|---|
| VPC | Aislamiento de red |
| ALB | Load balancing + TLS termination |
| NAT Gateway | Salida controlada a internet (proveedores) |
| VPC Endpoints | Acceso privado a S3, Secrets Manager, KMS |
| Route 53 | DNS management, wildcard, health checks |
| ACM | Certificados TLS auto-renovables |
CI/CD
| Servicio | Uso |
|---|---|
| GitHub Actions | Build, test, push to ECR |
| ECR | Container registry |
| ECS Deploy | Rolling deployment (via GitHub Actions) |
| AWS CDK | Infrastructure as Code (TypeScript) |
Sizing y Costos Estimados
Ambiente de Desarrollo/Staging
| Componente | Configuracion | Costo estimado/mes |
|---|---|---|
| ECS Fargate (6 servicios) | 0.25 vCPU, 0.5 GB cada uno | ~$50 |
| Aurora Serverless v2 | 0.5-2 ACU | ~$45 |
| ElastiCache Serverless | Baseline | ~$15 |
| Amazon MQ | mq.t3.micro single | ~$25 |
| ALB | 1 ALB | ~$20 |
| NAT Gateway | 1 AZ | ~$35 |
| S3 | Minimal | ~$5 |
| Total staging | ~$195/mes |
Ambiente de Produccion (inicial)
| Componente | Configuracion | Costo estimado/mes |
|---|---|---|
| ECS Fargate (6 servicios) | 0.5 vCPU, 1 GB, 2 tasks cada uno | ~$200 |
| Aurora Serverless v2 | 1-8 ACU, Multi-AZ | ~$150 |
| ElastiCache Serverless | Auto-scaling | ~$50 |
| Amazon MQ | mq.m5.large Multi-AZ | ~$200 |
| ALB | 1 ALB | ~$25 |
| NAT Gateway | 2 AZ | ~$70 |
| CloudFront | Moderate traffic | ~$30 |
| WAF | Core rules | ~$25 |
| S3 + KMS | Moderate storage | ~$20 |
| Secrets Manager | ~20 secrets | ~$10 |
| CloudWatch | Logs + metrics | ~$50 |
| Total produccion | ~$830/mes |
Nota: Costos estimados para us-east-1. Varian por region y uso real.
Auto-Scaling
ECS Fargate
yaml
# Cada servicio escala independientemente
scaling:
min_tasks: 2 # Minimo para alta disponibilidad
max_tasks: 10 # Maximo por servicio
target_cpu: 70% # Escalar cuando CPU > 70%
target_memory: 80% # Escalar cuando memoria > 80%
scale_in_cooldown: 300s
scale_out_cooldown: 60sAurora Serverless v2
yaml
scaling:
min_acu: 0.5 # Minimo (puede bajar a 0.5 en inactividad)
max_acu: 16 # Maximo (escala automaticamente)
# No requiere configuracion manual - escala por demandaAlta Disponibilidad
| Componente | Estrategia |
|---|---|
| ECS Fargate | Min 2 tasks en diferentes AZs |
| Aurora | Multi-AZ con failover automatico |
| ElastiCache | Multi-AZ con replicas |
| Amazon MQ | Multi-AZ deployment |
| ALB | Multi-AZ por defecto |
| S3 | 11 9s durabilidad, cross-AZ por defecto |
Disaster Recovery
| Escenario | RPO | RTO | Estrategia |
|---|---|---|---|
| Fallo de AZ | 0 | < 1 min | Multi-AZ automatico |
| Fallo de region | < 5 min | < 30 min | Aurora Global Database + ECS cross-region |
| Corrupcion de datos | < 1 hora | < 2 horas | Aurora point-in-time recovery |
| Eliminacion accidental | 0 | < 15 min | S3 versioning + Aurora backups |
Deployment Pipeline
Estrategia de Deploy
| Ambiente | Trigger | Estrategia | Rollback |
|---|---|---|---|
| Staging | Push to develop | Automatico, rolling update | Automatico si health check falla |
| Production | Push to main (via release) | Manual approval + rolling update | Automatico + manual option |
Rolling Update (ECS)
yaml
deployment:
minimum_healthy_percent: 100 # Nunca menos del 100% de tasks sanas
maximum_percent: 200 # Permite el doble durante deploy
# ECS lanza nuevas tasks, espera health check, drena las viejasInfrastructure as Code (AWS CDK)
Repositorio: imagy-infra
imagy-infra/
├── bin/
│ └── app.ts # Entry point
├── lib/
│ ├── stacks/
│ │ ├── networking-stack.ts # VPC, subnets, NAT, security groups
│ │ ├── data-stack.ts # Aurora, ElastiCache, Amazon MQ
│ │ ├── compute-stack.ts # ECS cluster, services, ALB
│ │ ├── security-stack.ts # WAF, KMS, Secrets Manager
│ │ ├── observability-stack.ts # CloudWatch, X-Ray, alarms
│ │ └── dns-stack.ts # Route 53, ACM certificates
│ └── constructs/
│ ├── fargate-service.ts # Construct reutilizable por servicio
│ └── aurora-database.ts # Construct para BD por dominio
├── config/
│ ├── staging.ts
│ └── production.ts
└── cdk.jsonFTR Compliance Checklist
| Requisito | Implementacion | Estado |
|---|---|---|
| Multi-AZ | Aurora, ElastiCache, MQ, ECS en 2+ AZs | Por diseño |
| Encryption at-rest | Aurora, S3, ElastiCache con KMS CMK | Por diseño |
| Encryption in-transit | TLS en todas las conexiones | Por diseño |
| Centralized logging | CloudWatch Logs con retention | Por diseño |
| Automated backups | Aurora automated + S3 versioning | Por diseño |
| IAM least privilege | Task roles por servicio, no access keys | Por diseño |
| No hardcoded credentials | Secrets Manager exclusivamente | Por diseño |
| Disaster recovery | Multi-AZ + point-in-time recovery | Por diseño |
| Security monitoring | GuardDuty + Security Hub | Por diseño |
| Incident response | Documented runbooks | Pendiente |
| Data residency | Region configurable por tenant | Por diseño |
| Vulnerability management | Dependabot + ECR scanning | Por diseño |