Following the Serverless framework documentation, I added this to my serverless.yaml
provider:
name: aws
apiGateway:
minimumCompressionSize: 1
So, I have any given endpoint for testing, it returns a JSON string of approximately 500 characters, which, according to my configuration above, should return compressed, but as we can verify through cURL, it does not come compressed.
functions:
v1-http-dummy:
handler: app/v1/http/dummy.handler
events:
- httpApi:
path: /v1/dummy
method: GET
Checking:
$ curl -X POST -sILH 'Accept-Encoding: *' https://example.com/v1/dummy
HTTP/2 404
date: Thu, 16 Nov 2023 10:19:13 GMT
content-type: application/json
content-length: 23
apigw-requestid: OfHpQg_xIAMEJwA=
Without any signs of compression.
I'm aware there is a difference between REST API and HTTP API; my project utilizes HTTP API. My question is, how can I enable compression on API Gateway using Serverless framework?
It could also be through CloudFormation.
I tried an answer I found on StackOverflow, but it doesn't work anymore, which is why I am asking again. Hence, this should not be considered a duplicate question because the old solution is no longer effective.
You need to explicitly specify that you want to receive compressed response: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-receive-response-with-compressed-payload.html
Also a note, if you want to enable compression for all requests/responses, you can set the value to 0
. Ref: https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html#minimumCompressionSize
EDIT: After our conversation, I've realised that you're using httpApi
event type, while apiGateway
settings on provided only apply to http
event type and compression setting is only available for http
event type. If your case, you could use the http
event instead of httpApi
like below:
functions:
v1-http-dummy:
handler: app/v1/http/dummy.handler
events:
- http:
path: v1/dummy
method: GET
For differences between httpApi
and http
events please see the docs:
http
event docs: https://www.serverless.com/framework/docs/providers/aws/events/apigatewayhttpApi
event docs: https://www.serverless.com/framework/docs/providers/aws/events/http-api