Bạn có 10 Lambda function. Làm sao user gọi đúng function? Ai xác thực request? Ai rate limit? API Gateway là reverse proxy managed: authentication, throttling, CORS, routing, không cần tự build.


  flowchart LR
    Client["Client + JWT"] --> APIG["API Gateway"]
    APIG --> JWT["JWT Authorizer<br/>Cognito / Auth0 / Okta"]
    APIG --> Lambda["Lambda<br/>business logic"]
    APIG --> SQS["SQS (proxy)<br/>async processing"]

REST vs HTTP: chọn đúng

REST APIHTTP API
Cost$3.50/1M$1.00/1M
Latency20-50ms5-15ms
AuthorizerCognito (JWT), Lambda, IAMJWT (any OIDC), Lambda, IAM
Dùng khiUsage plan, API key, VTLApp mới (recommended)
aws apigatewayv2 create-api --name my-http-api --protocol-type HTTP

JWT Authorizer + CORS + Throttling

aws apigatewayv2 create-authorizer --api-id xxx --authorizer-type JWT \
  --identity-source '$request.header.Authorization' \
  --jwt-configuration Audience=["xxx"],Issuer=https://cognito-idp.ap-southeast-1.amazonaws.com/...

aws apigatewayv2 update-api --api-id xxx --cors-configuration \
  AllowOrigins=["https://myapp.com"],AllowMethods=["GET","POST","PUT","DELETE"],AllowHeaders=["Content-Type","Authorization"]

# HTTP API throttling: cấu hình ở stage level, không phải API level
aws apigatewayv2 update-stage --api-id xxx --stage-name prod \
  --default-route-settings "ThrottlingRateLimit=1000,ThrottlingBurstLimit=500"

Dòng này tạo authorizer JWT với Cognito làm issuer, audience là client ID của app.

HTTP API tự động trả lời OPTIONS preflight — không cần route riêng. Mình từng mất nửa ngày vì quên CORS config trên REST API, chuyển sang HTTP API xong tự động có — tiết kiệm thời gian.


Custom domain

aws apigatewayv2 create-domain-name --domain-name api.myapp.com \
  --domain-name-configurations CertificateArn=$ACM_ARN
aws apigatewayv2 create-api-mapping --api-id xxx --domain-name api.myapp.com --stage prod

  • HTTP API rẻ hơn 70%, nhanh hơn REST — default cho app mới
  • JWT authorizer tích hợp Cognito — không custom Lambda
  • Throttling bắt buộc — bảo vệ backend
  • CORS auto với HTTP API — không OPTIONS route

Bài sau: Phần 17: VPC căn bản — subnet, route table, NAT, Internet Gateway