Arquitetura Web na AWS
Introdução
Fazer o deploy de um serviço web na AWS exige combinar vários serviços: VPC, EC2, Load Balancer e uma boa estratégia de segurança. Esta seção cobre os padrões de arquitetura mais comuns.
Padrão 1: Arquitetura Simples
Uma Instância EC2 Pública
┌─────────────────────────────────────────────────────────────┐
│ ARQUITETURA SIMPLES (dev/teste) │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ▼ │
│ ┌──────────┐ │
│ │ IGW │ │
│ └────┬─────┘ │
│ │ │
│ ┌─────────────────────┼─────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ┌─────────────────▼───────────────────┐ │ │
│ │ │ SUBNET PUBLICA │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────────┐ │ │ │
│ │ │ │ EC2 │ │ │ │
│ │ │ │ Web Server │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ IP: 52.1.2.3 │ │ │ │
│ │ │ │ SG: web-server │ │ │ │
│ │ │ └─────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └──────────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────┘ │
│ │
│ Vantagens: │
│ ✅ Simples de fazer deploy │
│ ✅ Custo mínimo │
│ ✅ Ideal para dev/teste │
│ │
│ Desvantagens: │
│ ❌ Sem alta disponibilidade │
│ ❌ Ponto único de falha │
│ ❌ Não é escalável │
│ │
└─────────────────────────────────────────────────────────────┘
Configuração
# 1. Criar um Security Group para o web
aws ec2 create-security-group \
--group-name sg-web-simple \
--description "Web server SG" \
--vpc-id vpc-xxx
# 2. Liberar HTTP e HTTPS
aws ec2 authorize-security-group-ingress \
--group-id sg-xxx \
--protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress \
--group-id sg-xxx \
--protocol tcp --port 443 --cidr 0.0.0.0/0
# 3. Iniciar a instância
aws ec2 run-instances \
--image-id ami-xxx \
--instance-type t3.micro \
--subnet-id subnet-public \
--security-group-ids sg-xxx \
--associate-public-ip-address \
--user-data '#!/bin/bash
yum install -y httpd
systemctl start httpd
echo "Hello World" > /var/www/html/index.html'
Padrão 2: Arquitetura com Load Balancer
ALB + Instâncias Privadas
┌─────────────────────────────────────────────────────────────────────────┐
│ ARQUITETURA COM ALB (produção) │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ▼ │
│ ┌──────────┐ │
│ │ IGW │ │
│ └────┬─────┘ │
│ │ │
│ ┌────────────────────────────┼────────────────────────────────┐ │
│ │ VPC │ (10.0.0.0/16) │ │
│ │ │ │ │
│ │ ┌────────────────────────┴────────────────────────────┐ │ │
│ │ │ SUBNETS PÚBLICAS │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────┐ ┌─────────────┐ │ │ │
│ │ │ │ Public 1a │ │ Public 1b │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌───────┐ │ │ ┌───────┐ │ │ │ │
│ │ │ │ │ ALB │◄─┼──────────────┼──│ ALB │ │ │ │ │
│ │ │ │ │ node │ │ │ │ node │ │ │ │ │
│ │ │ │ └───┬───┘ │ │ └───┬───┘ │ │ │ │
│ │ │ └──────┼──────┘ └──────┼──────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └─────────┼────────────────────────────┼───────────────┘ │ │
│ │ │ │ │ │
│ │ ┌─────────┼────────────────────────────┼───────────────┐ │ │
│ │ │ │ SUBNETS PRIVADAS │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌──────▼──────┐ ┌──────▼──────┐ │ │ │
│ │ │ │ Private 1a │ │ Private 1b │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌─────────┐ │ │ ┌─────────┐ │ │ │ │
│ │ │ │ │ EC2 │ │ │ │ EC2 │ │ │ │ │
│ │ │ │ │ Web 1 │ │ │ │ Web 2 │ │ │ │ │
│ │ │ │ └─────────┘ │ │ └─────────┘ │ │ │ │
│ │ │ └─────────────┘ └─────────────┘ │ │ │
│ │ │ │ │ │
│ │ └──────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────┘ │
│ │
│ Vantagens: │
│ ✅ Alta disponibilidade (Multi-AZ) │
│ ✅ Escalável (adicionar instâncias) │
│ ✅ SSL termination no nível do ALB │
│ ✅ Servidores web protegidos (subnet privada) │
│ │
└─────────────────────────────────────────────────────────────────────────┘
Tipos de Load Balancers AWS
| Tipo | Uso | Camada OSI | Protocolos |
|---|---|---|---|
| ALB | Aplicações HTTP/HTTPS | Camada 7 | HTTP, HTTPS, gRPC |
| NLB | Alta performance | Camada 4 | TCP, UDP, TLS |
| CLB | Legado | Camada 4/7 | TCP, SSL, HTTP, HTTPS |
| GWLB | Appliances de rede | Camada 3 | IP |
Configuração ALB
# 1. Criar o Target Group
aws elbv2 create-target-group \
--name tg-web-servers \
--protocol HTTP \
--port 80 \
--vpc-id vpc-xxx \
--health-check-path /health \
--health-check-interval-seconds 30
# 2. Criar o Application Load Balancer
aws elbv2 create-load-balancer \
--name alb-web \
--subnets subnet-public-1a subnet-public-1b \
--security-groups sg-alb \
--scheme internet-facing \
--type application
# 3. Criar o Listener
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:...:alb-web \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:...:tg-web-servers
# 4. Registrar as instâncias
aws elbv2 register-targets \
--target-group-arn arn:aws:...:tg-web-servers \
--targets Id=i-web1 Id=i-web2
Padrão 3: Arquitetura 3-Tiers
Web + App + Banco de Dados
┌─────────────────────────────────────────────────────────────────────────┐
│ ARQUITETURA 3-TIERS │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ┌──────▼──────┐ │
│ │ IGW │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────────────┼──────────────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ╔══════════════════════════╧════════════════════════════╗ │ │
│ │ ║ TIER 1 : APRESENTAÇÃO ║ │ │
│ │ ║ (Subnet Pública) ║ │ │
│ │ ║ ║ │ │
│ │ ║ ┌─────────────────────────┐ ║ │ │
│ │ ║ │ Application Load │ ║ │ │
│ │ ║ │ Balancer (ALB) │ ║ │ │
│ │ ║ └───────────┬─────────────┘ ║ │ │
│ │ ╚═══════════════════════╪═══════════════════════════════╝ │ │
│ │ │ │ │
│ │ ╔═══════════════════════╪═══════════════════════════════╗ │ │
│ │ ║ TIER 2 : APLICAÇÃO ║ │ │
│ │ ║ (Subnet Privada App) ║ │ │
│ │ ║ │ ║ │ │
│ │ ║ ┌────────────────────┼────────────────────┐ ║ │ │
│ │ ║ │ │ │ ║ │ │
│ │ ║ ┌───▼───┐ ┌─────▼─────┐ ┌─────▼───┐ ║ │ │
│ │ ║ │ EC2 │ │ EC2 │ │ EC2 │ ║ │ │
│ │ ║ │ App 1 │ │ App 2 │ │ App 3 │ ║ │ │
│ │ ║ └───┬───┘ └─────┬─────┘ └────┬────┘ ║ │ │
│ │ ║ │ │ │ ║ │ │
│ │ ╚══════╪════════════════════╪═══════════════════╪═══════╝ │ │
│ │ │ │ │ │ │
│ │ ╔══════╪════════════════════╪═══════════════════╪═══════╗ │ │
│ │ ║ │ TIER 3 : DADOS │ │ ║ │ │
│ │ ║ │ (Subnet Privada DB) │ ║ │ │
│ │ ║ │ │ │ ║ │ │
│ │ ║ └────────────────────┼───────────────────┘ ║ │ │
│ │ ║ │ ║ │ │
│ │ ║ ┌──────▼──────┐ ║ │ │
│ │ ║ │ RDS │ ║ │ │
│ │ ║ │ MySQL │ ║ │ │
│ │ ║ │ Multi-AZ │ ║ │ │
│ │ ║ └─────────────┘ ║ │ │
│ │ ║ ║ │ │
│ │ ╚════════════════════════════════════════════════════════╝ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
Security Groups para 3-Tiers
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUPS 3-TIERS │
├─────────────────────────────────────────────────────────────┤
│ │
│ sg-alb (Load Balancer) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: HTTP/443 from 0.0.0.0/0 │ │
│ │ Outbound: HTTP/80 to sg-app │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ sg-app (Application Servers) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: HTTP/80 from sg-alb │ │
│ │ Inbound: SSH/22 from sg-bastion │ │
│ │ Outbound: MySQL/3306 to sg-db │ │
│ │ Outbound: HTTPS/443 to 0.0.0.0/0 (APIs externas) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ sg-db (Database) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: MySQL/3306 from sg-app │ │
│ │ Outbound: Nenhum (ou mínimo) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ Princípio: Cada tier aceita SOMENTE tráfego do tier │
│ anterior │
│ │
└─────────────────────────────────────────────────────────────┘
Health Checks
Configuração Health Check ALB
┌─────────────────────────────────────────────────────────────┐
│ HEALTH CHECKS ALB │
├─────────────────────────────────────────────────────────────┤
│ │
│ Parâmetros importantes: │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Protocol : HTTP │ │
│ │ Path : /health ou /api/health │ │
│ │ Port : traffic-port (80) │ │
│ │ Healthy threshold : 2 (sucessos consecutivos) │ │
│ │ Unhealthy threshold : 3 (falhas consecutivas) │ │
│ │ Timeout : 5 seconds │ │
│ │ Interval : 30 seconds │ │
│ │ Success codes : 200-299 │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ Endpoint /health típico: │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ HTTP 200 OK │ │
│ │ { │ │
│ │ "status": "healthy", │ │
│ │ "database": "connected", │ │
│ │ "cache": "connected" │ │
│ │ } │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
DNS com Route 53
┌─────────────────────────────────────────────────────────────┐
│ ROUTE 53 + ALB │
├─────────────────────────────────────────────────────────────┤
│ │
│ Usuário digita: www.monsite.com │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Route 53 │ │
│ │ DNS │ │
│ └──────┬───────┘ │
│ │ │
│ │ Alias Record │
│ │ www.monsite.com │
│ │ → alb-web-xxx.elb.amazonaws.com│
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ ALB │ │
│ └──────────────┘ │
│ │
│ Tipos de registros: │
│ • A (Alias): Aponta para recurso AWS (ALB, CF, S3) │
│ • CNAME : Aponta para outro nome de domínio │
│ │
│ Vantagem do Alias: │
│ ✅ Gratuito para requisições a recursos AWS │
│ ✅ Responde com os IPs diretamente │
│ │
└─────────────────────────────────────────────────────────────┘
Resumo dos Padrões
| Padrão | Complexidade | Custo | HA | Caso de uso |
|---|---|---|---|---|
| EC2 Simples | ⭐ | $ | ❌ | Dev/Teste |
| ALB + EC2 | ⭐⭐ | $$ | ✅ | Produção simples |
| 3-Tiers | ⭐⭐⭐ | $$$ | ✅✅ | Produção |
| Microservices | ⭐⭐⭐⭐ | $$$$ | ✅✅✅ | Grande escala |
Próxima seção: Lab: Publicação Web