3.3.2 Deploy API Resources to Environments

Deploy API Resources to Various Environments

API Gateway Stages

/dev    → Lambda alias: dev    → DynamoDB: orders-dev
/staging → Lambda alias: staging → DynamoDB: orders-staging
/prod   → Lambda alias: prod   → DynamoDB: orders-prod

Stage Variables

# Stage: dev
lambdaAlias: dev
tableName: orders-dev
logLevel: DEBUG

# Stage: prod
lambdaAlias: prod
tableName: orders-prod
logLevel: ERROR

Custom Domain Names

api.example.com → API Gateway Custom Domain
  /v1 → API Gateway Stage: v1
  /v2 → API Gateway Stage: v2
  • ACM certificate required
  • Route 53 alias record → API Gateway domain
  • Base path mapping: URL path → stage

Amazon Route 53 cho Developer

Route 53 là DNS service dùng để route traffic đến AWS resources.

Record TypeUse Case
A (Alias)API Gateway custom domain, CloudFront, ALB, S3 website
CNAMENon-root domain → another domain
AliasRoot domain (zone apex) → AWS resource

Routing Policies cho Deployment

PolicyUse Case
SimpleSingle resource
WeightedCanary/gradual deployment (90% old, 10% new)
FailoverActive-passive (disaster recovery)
Latency-basedRoute to lowest latency region
GeolocationRoute by user location
# Weighted routing cho canary deployment
api.example.com → 90% → API Gateway v1 (current)
                → 10% → API Gateway v2 (new)

Health Checks

  • Monitor endpoint health
  • Automatic failover khi unhealthy
  • Integration với CloudWatch Alarms

SAM API Deployment

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage
      Variables:
        TableName: !Sub "orders-${Stage}"

Exam Tip: Stage variables for per-environment config. Custom domains for production APIs. Base path mapping for API versioning.