Sub topic 2
HTTP API or REST API: choosing between them
This chapter assumes you know what a route, an integration and a stage are from the earlier chapters. It also assumes you can read a SAM template. API Gateway offers two products for building HTTP-style APIs in front of Lambda. Both can run your handlers, but they differ in price, features and event format. You make this choice early because moving an API later means changing resource types, event shapes and often your authorization setup.
The two products
Amazon API Gateway has three API types: HTTP API, REST API and WebSocket API. This chapter compares the first two.
- REST API is the original product. It has the larger feature set: request validation, caching, API keys, usage plans, mapping templates and private endpoints.
- HTTP API is the newer, simpler product. It costs less, adds less latency, and gives you built-in CORS, JWT authorization and automatic deployments.
The names are misleading. An HTTP API is not "more HTTP" than a REST API. Both serve ordinary HTTP requests with JSON bodies. "REST API" is a product name, not a statement about your design.
Feature comparison
The table lists the differences that most often decide the choice. Quotas and prices change, so confirm them against the current AWS documentation before you commit.
| Concern | HTTP API | REST API |
|---|---|---|
| Price per million requests | Lower (roughly a third of REST in common regions) | Higher |
| Added latency | Lower | Higher |
| Lambda event format | Version 2.0 (1.0 also available) | Version 1.0 only |
| JWT authorizer | Built in | Not built in (use a Lambda authorizer, or Cognito user pool authorizer) |
| Lambda authorizer | Yes, with optional simple responses | Yes, returns an IAM policy |
| IAM authorization | Yes | Yes |
| Cognito user pool authorizer | Through the JWT authorizer | Dedicated authorizer type |
| API keys and usage plans | No | Yes |
| Request validation | No | Yes |
| Mapping templates (VTL) | No (parameter mapping only) | Yes |
| Response caching | No | Yes |
| AWS WAF | No | Yes |
| Resource policies | No | Yes |
| Endpoint types | Regional | Edge-optimized, Regional, private |
| Canary deployments | No | Yes |
| Deployment model | Auto-deploy can be enabled per stage | Explicit deployment required |
| CORS | Built-in configuration | Manual (OPTIONS methods and response headers) |
How the choice flows
Start from the features you cannot do without. Cost and simplicity break ties.
Where HTTP API is the better fit
For a typical Lambda backend, HTTP API is the right default. It fits when:
- Your handlers do their own validation and shape their own responses.
- Your clients are web or mobile apps that send a bearer token. A JWT authorizer validates tokens from Amazon Cognito or any OpenID Connect provider without any code on your side.
- Cost matters at volume. The per-request price difference grows with traffic.
- You want less configuration. CORS is a few lines in the template, and the
$defaultstage can deploy automatically.
Where REST API is required
Choose a REST API when any of these apply:
- Third-party access control. API keys and usage plans let you issue keys to partners and cap their quotas. HTTP APIs have no equivalent. Note that API keys identify callers for metering. They are not a security mechanism on their own.
- AWS WAF. You can attach a web ACL to a REST API stage. An HTTP API cannot have one attached directly. You can put CloudFront in front of it and attach WAF there, at the cost of more moving parts.
- Private APIs. Only REST APIs can be reached solely through an interface VPC endpoint.
- Caching. A REST API stage can cache responses so repeated requests skip the backend.
- Payload transformation without code. Mapping templates reshape requests and responses, which is useful when you integrate directly with other AWS services or legacy backends.
- Request validation at the gateway. A REST API can reject malformed requests against a JSON schema before Lambda runs, so you do not pay for those invocations.
What changes in your code
The product you pick changes the shape of the event your handler receives. HTTP API with payload format 2.0 puts the method and path under requestContext.http and the route in routeKey. REST API uses httpMethod and resource at the top level, and delivers header values as both single and list forms.
If you must support both during a migration, a small adapter keeps the rest of the handler unchanged:
def get_method_and_path(event):
http = event.get("requestContext", {}).get("http")
if http:
# HTTP API, payload format 2.0
return http["method"], event["rawPath"]
# REST API, or HTTP API payload format 1.0
return event["httpMethod"], event["path"]
def handler(event, context):
method, path = get_method_and_path(event)
return {
"statusCode": 200,
"headers": {"content-type": "application/json"},
"body": f'{{"method": "{method}", "path": "{path}"}}',
}The response also differs slightly. A version 2.0 handler may return a plain dictionary, and API Gateway infers a 200 response with a JSON content type. A REST API proxy integration requires the full statusCode, headers and body structure, or it returns a 502 error. Returning the full structure in every case, as above, works for both.
What changes in SAM
SAM uses separate resource types and event types for each product.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless::Application
Resources: {}That fragment is only a reminder of the header. The meaningful difference is in the resources. Here is an HTTP API version of a function with one route:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HttpApi:
Type: AWS::Serverless::HttpApi
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.13
Handler: app.handler
CodeUri: src/
Events:
Hello:
Type: HttpApi
Properties:
ApiId: !Ref HttpApi
Path: /hello
Method: GETAnd the REST API equivalent:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
RestApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.13
Handler: app.handler
CodeUri: src/
Events:
Hello:
Type: Api
Properties:
RestApiId: !Ref RestApi
Path: /hello
Method: GETNote the differences. The resource is AWS::Serverless::HttpApi or AWS::Serverless::Api. The event type is HttpApi or Api. The property is ApiId or RestApiId. The REST version also requires StageName. Mixing these (an Api event pointing at an HttpApi) fails at deploy time.
Common errors when choosing wrongly
- Expecting API keys on an HTTP API. There is no
ApiKeyRequiredsetting. Move to a REST API, or authenticate with a JWT or Lambda authorizer instead. - Writing a handler for the wrong event. A handler that reads
event["httpMethod"]raisesKeyErroron version 2.0 events, and the API returns a 500 error. Check the CloudWatch Logs for the traceback. - Assuming the REST API timeout can always be raised. Integration timeouts default to 29 seconds on REST and have a 30-second ceiling on HTTP API. REST Regional APIs can request higher limits, which may reduce your throttle quota. Long work belongs in an asynchronous pattern, not a longer timeout.
- Treating the choice as reversible by renaming. The two products have different resource IDs, URLs and stage behavior. Plan a migration with a new URL and a custom domain cutover, not an in-place switch.
Exercise
A team builds a public API for paying partners. Each partner gets a key, and each key has a daily request quota. The team also wants AWS WAF rules to block known bad IP ranges. Which API type fits, and why?
Show answer
A REST API. API keys with usage plans (per-key quotas) and direct AWS WAF association are REST API features that an HTTP API does not provide. Keys only meter and identify callers, so the team should still add a real authorizer for security.
Exercise
Your Lambda handler returns a correct response but the route fails with a KeyError on event["httpMethod"] after you move from a REST API to an HTTP API with payload format 2.0. What is the fix?
Show answer
Read the method from event["requestContext"]["http"]["method"] (and the path from event["rawPath"]), since version 2.0 events do not have the top-level httpMethod and path fields. Alternatively, use event["requestContext"]["routeKey"] to branch.