Deploying a Web API on AWS Lambda: Difference between API Gateway and Application Load Balancer
Tags: aws, serverless
Published: 2019-09-07 23:25:43

When we deploy a web API on AWS Lambda leveraging the benefit of server less architecture, we have two options for the front-end service for Lambda: API Gateway (API GW)  or Application Load Balancer (ALB). This articles studies the difference between them to help you to decide which to choose.

##### Features unique to API Gateway

- It is obvious, but API Gateway specific features such as [Request Validation](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-method-request-validation.html), [Data Mapping](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/mappings.html), [Access Control](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html) and [SDK Generation](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/how-to-generate-sdk.html) cannot be used with ALB.

##### Protocols and Port Numbers

* API GW supports only HTTPS (TLS 1.2) on 443 port. Note that though API GW supports [VPC Endpoint](https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/apigateway-private-apis.html), you still need to use HTTPS in this case too.
* ALB supports both HTTP and HTTPS on arbitrary port numbers and TLS version also can be chosen from various [options](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html#describe-ssl-policies).

##### Request Timeout

* API GW has a severe limitation of [29 seconds](https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-limits).
* ALB itself doesn't have a limitation, thus [the timeout of Lambda](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#prepare-lambda-function) (up to 15 minutes) is applied.

##### Request and Response Sizes

* API GW can handle up to 6MB payload, which is [the ceiling of Lambda's payload size](https://docs.aws.amazon.com/lambda/latest/dg/limits.html).
  * Note that 6MB includes headers and Base64-encoded body as payloads are requests and responses of Lambda.
  * The limitation of API GW's payload size is [10MB](https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-limits).

* With ALB, the size of request and response bodies cannot be larger than 1MB. [413 error](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-troubleshooting.html#http-413-issues) will be returned when a request body exceeds the limit, and [502 error](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-troubleshooting.html#http-502-issues) for response body.

##### Request Event（[Reference](https://serverless-training.com/articles/api-gateway-vs-application-load-balancer-technical-details/#application-load-balancer-request-event-format-differences)）

* evt.headers (Request)
  * API GW sets header names and values with keeping the original upper and lower cases, whereas ALB decapitalize header names.
* evt.queryStringParameters (Request)
  * When there is no query string parameter, API GW sets `null`, whereas ALB sets `{}`.
  * API Gateway sets the URL decoded value for query string parameter key and value, whereas ALB sets them without URL decoding.
* evt.multiValueHeaders / evt.multiValueQueryStringParameters (Request)
  * API Gateway sets these parameters by default, whereas ALB requires that they are enabled in its configuration.

##### Response Event（[Reference](https://serverless-training.com/articles/api-gateway-vs-application-load-balancer-technical-details/#application-load-balancer-response-event-format-differences)）

*  ALB response event must include statusDescription, isBaseEncoded and headers (when "Multi Value Header" is enabled in ALB configuration).

##### Cost

* Up to a certain request frequency, API GW is cheaper than ALB, that has a time-based charge. However for the services that receive a large amount of requests, ALB is more reasonable. ([Reference](https://serverless-training.com/articles/save-money-by-replacing-api-gateway-with-application-load-balancer/))

#### Other References

* [Amazon Web Services Blog](https://aws.amazon.com/jp/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/)
  * It only mentions as follows:`there’s also a lot of overlap and commonality between the parameters used by Amazon API Gateway for triggering Lambda functions and ALB’s parameters.`
* [API Gateway vs Application Load Balancer—Technical Details](https://serverless-training.com/articles/api-gateway-vs-application-load-balancer-technical-details/)
* [API Gateway vs. ALB - 持田システム](http://www.motidasystem.com/tech/aws/201901271852.html)
  * It covers the difference in request timeout and limitation on response size.