AWS Esencial
6.1.5kata 5 de 5

Control de Costos y Presupuestos

Monitorear, alertar y optimizar gastos en AWS

El modelo de costo de AWS es 'pay-as-you-go': pagas por lo que usas, sin compromisos mínimos (a menos que compres Reserved Instances o Savings Plans). Los costos principales para IA son: (1) compute (EC2, SageMaker, Lambda) — caro, especialmente con GPUs, (2) almacenamiento (S3) — barato por GB pero suma si no se archiva, (3) transferencia de datos (Data Transfer OUT) — muy caro si cross-region, (4) modelos managedos (Bedrock, SageMaker Endpoints) — por invocación o por hora. Un error frecuente: dejar un endpoint ml.p3.2xlarge activo sin tráfico = $2+/hora = $1500+/mes.

AWS Cost Explorer es tu aliado: dashboard que desglose costos por servicio, región, etiqueta. Configurar alertas de presupuesto (`aws budgets create-budget`) notifica cuando gastos exceden un umbral. CloudWatch Alarms en métricas de costo también funciona. Sin monitoreo, facturas sorpresas es garantizado.

Optimizaciones: (1) usar Spot Instances para training (70% discount pero pueden interrumpirse), (2) apagar recursos cuando no se usan (EC2 stop, no terminate), (3) usar Batch Transform en lugar de Endpoints para inferencia offline, (4) Serverless Inference en lugar de Endpoints siempre-activos para tráfico bajo, (5) taggear todos los recursos para fácil auditoría.

structure.txt
# View costs in Cost Explorer (CLI)
aws ce get-cost-and-usage \
  --time-period Start=2024-01-01,End=2024-01-31 \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=DIMENSION,Key=SERVICE

# Create budget with alert
aws budgets create-budget \
  --account-id 123456789 \
  --budget '{
    "BudgetName": "ML-Project-Budget",
    "BudgetLimit": {"Amount": "1000", "Unit": "USD"},
    "TimeUnit": "MONTHLY",
    "BudgetType": "COST"
}'

# Create notification for budget
aws budgets create-notification \
  --account-id 123456789 \
  --budget-name ML-Project-Budget \
  --notification '{
    "NotificationType": "FORECASTED",
    "ComparisonOperator": "GREATER_THAN",
    "Threshold": 80
}' \
  --notification-type Email

# Tag resources for cost allocation
aws ec2 create-tags \
  --resources i-0123456789abcdef \
  --tags Key=Project,Value=ML-Training Key=Env,Value=Prod

Debugging lab

Detecta y corrige el error en el código.

0/5 tests passing0%
  1. 01

    # SageMaker endpoint ml.p3.2xlarge running 24/7 # Only 10 requests/day # Cost: $2.00/hour * 720 hours/month = $1440/month # Actual inference time: ~10 min/month

  2. 02

    # EC2 instance t3.micro launched for testing # Forgot to terminate # 2 weeks later: $5 charge for a test

  3. 03

    # SageMaker training job running on ml.p3.2xlarge # Cost: $3.06/hour # Training takes 5 hours: $15.30 per run # Daily runs: $460/month

  4. 04

    # No tags on resources # Cost Explorer shows: $2000/month # But which project? Which environment?

  5. 05

    # S3 bucket stores 3 years of logs # All in S3 Standard ($0.023/GB) # 1TB of logs: $23/month # 95% acceso: 'never' (archive) or 'rarely' (once a month)