Lesson 1 of 3 Serverless and API Architecture

Choosing an API Front Door: API Gateway, AppSync or ALB

This lesson explains how the choice of an application's entry point shapes security, latency, cost and how clients evolve, and how to choose among AWS's overlapping front doors from requirements.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between REST, HTTP and WebSocket APIs, AppSync and an Application Load Balancer.
  • Choose an endpoint type, including private APIs.
  • Explain the authorisation, throttling and caching options.
  • Explain how long-running requests are handled asynchronously.

Choosing a front door

APIs can be exposed through five front doors. An API Gateway REST API has the richest management features. Usage plans and API keys, request validation, caching, WAF, resource policies, private APIs, canary stages and mutual TLS suit public or partner APIs that need governance per client. An API Gateway HTTP API costs less and adds less latency. It offers JWT authorizers and simple Lambda and HTTP proxies, suiting internal or simple APIs that do not need REST features. An API Gateway WebSocket API keeps persistent two-way connections, for chat and live dashboards.

AWS AppSync provides managed GraphQL, with real-time subscriptions, offline synchronisation and queries that combine many data sources, which suits mobile and web front ends. An Application Load Balancer handles very high throughput at a low cost per request. It has Lambda or container targets and built-in OIDC authentication, and suits steady, high-volume traffic where API management features are unnecessary. The following table summarises the options.

OptionStrengthsTypical use
API Gateway REST APIUsage plans and API keys, request validation, caching, WAF, resource policies, private APIs, canary stages, mutual TLSPublic or partner APIs needing governance per client
API Gateway HTTP APILower cost and latency, JWT authorizers, simple Lambda and HTTP proxiesInternal or simple APIs where REST features are not needed
API Gateway WebSocket APIPersistent two-way connectionsChat, live dashboards
AWS AppSyncManaged GraphQL with real-time subscriptions, offline sync, many data sources in one queryMobile and web front ends aggregating data
Application Load BalancerVery high throughput at low per-request cost, Lambda or container targets, OIDC authenticationSteady, high-volume traffic where API management features are unnecessary

Endpoint types and private APIs

REST APIs come in three endpoint types. Edge-optimised endpoints route clients through CloudFront points of presence, which helps geographically dispersed clients. Regional endpoints serve clients in the same Region and let you put your own CloudFront distribution and WAF in front. Private endpoints are reachable only through an interface VPC endpoint, and a resource policy decides which VPCs, endpoints or accounts may call them. Private APIs are the answer whenever an API must never be reachable from the internet.

Authorisation, throttling and caching

API Gateway authorises callers in several ways, chosen by who the caller is. AWS workloads sign requests with IAM (SigV4), so access is controlled by IAM policies. End users present tokens from Amazon Cognito user pools, or JWTs from any OpenID Connect provider on HTTP APIs. Lambda authorizers validate custom tokens or headers with code, and their results are cached for a configurable period to reduce latency and cost.

Throttling protects back ends at several levels: the account, each stage and method, and, through usage plans, each client identified by an API key, which can also have a daily, weekly or monthly quota. Clients that exceed a limit receive HTTP 429 and should retry with backoff. API keys identify clients for these plans but do not authenticate them. REST API stage caching, from 0.5 GB to 237 GB, serves repeated GET requests without calling the back end, keyed on chosen parameters and headers. The default integration timeout of 29 seconds can be raised for Regional and private REST APIs, but work that takes minutes belongs in an asynchronous pattern rather than a longer timeout.

29s

Default integration timeout

237GB

Largest REST API stage cache

Handling long-running requests

When a request triggers work that takes minutes, keeping the client waiting is fragile and expensive. The standard pattern returns immediately with a job ID: API Gateway sends the request to SQS or starts a Step Functions execution through a direct service integration, and the client polls a status endpoint, receives a WebSocket or AppSync notification, or gets a callback when the work completes.

Worked example

A private partner API with per-partner quotas

A logistics firm exposes a shipment API to 40 partners. Partners call over the internet, each with its own contractual limit, and the firm's internal systems must call a separate administrative API that must never be public. The design uses two REST APIs. The partner API is Regional, behind WAF, with one usage plan per contract tier:

aws apigateway create-usage-plan --name gold --api-stages apiId=$PARTNER_API,stage=prod \
  --throttle rateLimit=200,burstLimit=400 --quota limit=5000000,period=MONTH
aws apigateway create-usage-plan-key --usage-plan-id $GOLD_PLAN --key-id $PARTNER_42_KEY --key-type API_KEY

API keys identify partners for quotas, but they are not a security mechanism, so partners also authenticate with OAuth tokens checked by a Cognito or Lambda authorizer, or with mutual TLS on a custom domain. The administrative API is private, with a resource policy that admits only the firm's VPC endpoint:

{
  "Version": "2012-10-17",
  "Statement": [
    { "Effect": "Allow", "Principal": "*", "Action": "execute-api:Invoke", "Resource": "execute-api:/*" },
    { "Effect": "Deny", "Principal": "*", "Action": "execute-api:Invoke", "Resource": "execute-api:/*",
      "Condition": { "StringNotEquals": { "aws:SourceVpce": "vpce-0admin1234567890" } } }
  ]
}
Check yourself

When is AppSync the better front door?

Show answer

When clients need GraphQL to fetch exactly the fields they need from several sources in one request, or real-time updates through subscriptions.

Exam Tips

Remember for the exam

  • For per-client throttling and quotas, use REST API usage plans with API keys; keys identify, they do not authenticate.
  • For an API that must not be reachable from the internet, use a private REST API with a resource policy on aws:SourceVpce.
  • For GraphQL with real-time subscriptions, use AppSync.
  • For the lowest-cost simple API with JWT authorisation, use an HTTP API.
  • For long-running work, return immediately and process asynchronously with SQS or Step Functions.
  • For very high, steady request volumes without API management needs, an ALB in front of Lambda or containers can be cheaper.