API Gateway — Autenticación y Rate Limiting
Securizar y proteger endpoints públicos
API Gateway sin autenticación = endpoint público accesible a cualquiera. Para aplicaciones de ML, esto significa: anyone puede llamar SageMaker Endpoint 1000 veces/segundo y facturar miles a tu cuenta. Autenticación options: (1) API Key (simple, no user identity, bueno para terceros con cuota), (2) AWS IAM (solo usuarios AWS autorizados), (3) Cognito (user pools, gestiona identidad), (4) Lambda authorizers (custom logic).
Rate limiting controla cuántas requests por segundo/día se permiten. Sin rate limiting, un atacante puede: (1) generar costos masivos (1M requests a SageMaker), (2) saturar el endpoint (denial of service). API Gateway Usage Plans controlan cuotas: 'user X puede hacer 1000 requests/día, 100/min burst'.
Combinación: API Key + Rate Limiting es el baseline. Usuarios obtienen una API Key, API Gateway valida la key y aplica cuota. SageMaker solo procesa requests validadas, factura solo a usuarios autorizados.
# Create API Gateway with API Key auth
# In SAM template (template.yaml):
ApiAuth:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
ApiKeyRequired: true # Require API Key
# Create API Key for client
aws apigateway create-api-key \
--name client-api-key \
--enabled
# Create Usage Plan
aws apigateway create-usage-plan \
--name ml-api-plan \
--description 'Rate limit: 1000 req/day, 100 req/min' \
--throttle-settings BurstLimit=100,RateLimit=10 \
--quota-settings Limit=1000,Period=DAY
# Associate key to plan
aws apigateway create-usage-plan-key \
--usage-plan-id PLAN_ID \
--key-id API_KEY_ID \
--key-type 'API_KEY'Debugging lab
Detecta y corrige el error en el código.
- 01
# API Gateway endpoint sin autenticación POST https://api.example.com/predict # Returns predictions from SageMaker # No auth, no rate limiting
- 02
# Malicious user obtains API Key # Makes 100,000 requests/day # Bill: $10,000 from SageMaker
- 03
# Usage Plan with very permissive rate limit # ThrottleSettings: BurstLimit=10000, RateLimit=1000 # Intentional or misconfiguration?
- 04
# Client reports 429 Too Many Requests # They're within daily limit but bursting
- 05
# API Gateway + SageMaker Endpoint # Endpoint charges $2/hour (always on) # But only 10 requests/day