Docs
/
AWS Cloud
Chapter 17

17 — Serverless Architectures

Full-Stack Serverless

Frontend (S3 + CloudFront)
    ↓
API Gateway
    ↓
Lambda Functions
    ↓
DynamoDB / Aurora Serverless / S3
    ↓
SQS / EventBridge (async processing)

Zero servers to manage. Pay only for what you use. Auto-scales to any load.


Common Patterns

1. REST API

Client → API Gateway → Lambda → DynamoDB

2. Async Processing

Client → API Gateway → Lambda → SQS → Lambda (worker) → DynamoDB
                         ↓
                    Return 202 Accepted immediately

3. Event-Driven

S3 upload → Lambda (resize image) → S3 (thumbnails)
                                   → DynamoDB (metadata)

DynamoDB Stream → Lambda → Elasticsearch (sync search index)
                         → SNS (send notification)

4. Scheduled Tasks

EventBridge (cron) → Lambda → Generate report → S3
                             → Send email via SES

Step Functions (Workflows)

Orchestrate multi-step serverless workflows with visual state machines.

{
  "StartAt": "ValidateOrder",
  "States": {
    "ValidateOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...:validateOrder",
      "Next": "ProcessPayment"
    },
    "ProcessPayment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...:processPayment",
      "Next": "CheckPaymentResult"
    },
    "CheckPaymentResult": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.paymentStatus",
          "StringEquals": "success",
          "Next": "FulfillOrder"
        }
      ],
      "Default": "PaymentFailed"
    },
    "FulfillOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...:fulfillOrder",
      "End": true
    },
    "PaymentFailed": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...:handleFailure",
      "End": true
    }
  }
}
ValidateOrder → ProcessPayment → Success? → FulfillOrder
                                    ↓ No
                              PaymentFailed

AppSync (Managed GraphQL)

Client → AppSync (GraphQL) → DynamoDB resolvers (direct, no Lambda)
                             → Lambda resolvers (complex logic)
                             → HTTP resolvers (external APIs)

Features:
  - Real-time subscriptions (WebSocket)
  - Offline sync
  - Built-in caching
  - Cognito/API key/IAM auth

Amplify (Full-Stack Framework)

npm install -g @aws-amplify/cli
amplify init
amplify add api          # REST or GraphQL
amplify add auth         # Cognito
amplify add storage      # S3 or DynamoDB
amplify add hosting      # CloudFront + S3
amplify push             # Deploy everything

Best for: Rapid prototyping and frontend-focused teams.


Cost Optimization

Serverless cost model:
  Lambda:      $0.20 per 1M requests + $0.0000166667/GB-sec
  API Gateway: $1.00 per 1M requests (HTTP API)
  DynamoDB:    $1.25 per 1M writes, $0.25 per 1M reads (on-demand)
  S3:          $0.023/GB/month (Standard)

Optimization tips:
  ✅ Right-size Lambda memory (power tuning tool)
  ✅ Use DynamoDB on-demand for variable traffic
  ✅ Use HTTP API (not REST API) for simple proxies
  ✅ Cache with CloudFront / API Gateway caching
  ✅ Use S3 lifecycle policies for old data
  ✅ Batch SQS messages (reduce Lambda invocations)
  ✅ Use Provisioned Concurrency only for latency-critical functions

Serverless vs Containers Decision

FactorServerless (Lambda)Containers (ECS/K8s)
Startup timeCold start (100ms-2s)Always running
Max duration15 minutesUnlimited
Scaling speedInstant (ms)Minutes
Idle cost$0Minimum instances running
ComplexityLowMedium-High
Vendor lock-inHigherLower
Best forEvent-driven, APIs, variable trafficLong-running, high-throughput, complex apps

Key Takeaways

  • Serverless = zero servers, auto-scaling, pay-per-use
  • Use Lambda + API Gateway + DynamoDB as the core serverless stack
  • Step Functions for multi-step workflows (order processing, ETL pipelines)
  • AppSync for managed GraphQL with real-time subscriptions
  • Use async patterns (SQS, EventBridge) to decouple and handle spikes
  • Serverless is cheapest at low-to-medium traffic; containers win at high scale
  • Right-size Lambda memory — it affects both performance and cost