Accept GZip compressed body in AWS Lambda through AWS API Gateway

1.8k Views Asked by At

I have constructed an API with AWS API Gateway that is used to call an AWS Lambda function to do some serverless computing.

The payload size that I am passing through is too great (over 10 mb) so I have looked into compressing the payload clientside through the Javascript library pako making use of its gzip method. This outputs a compressed Unit8Array that I then pass through the body and make an API request call with fetch().

Now for some reason, AWS API Gateway does not acknowledge this and simply fails. There is no log output in Cloudwatch for the POST method and there is no response returned that might indicate what went wrong.

I have worked through https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-gzip-compression-decompression.html , https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-enable-compression.html , https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-make-request-with-compressed-payload.html but neither of these tutorials have helped sadly.

I have even tried passing through the exact same example shown in the 3rd link (by generating a gzip of the JSON Object they construct) and I have been attempting my own tests and this still has not succeeded in being recognised. I do believe this means that I must have missed something in the AWS API Gateway but I am very unsure of what. If anyone has any thoughts, thanks in advance.

I have attempted (and others):

  • Introducing Binary-Media Types in the settings of the API Gateway
  • Using AWS Lambda Proxy Integration so the request is sent through. The issue here is that when trying to convert I am getting Type Errors in Python stating it expected a Binary Type not Str despite using the correct methods to convert.
var myHeaders = new Headers();
    myHeaders.append("Content-Encoding", "gzip");
    myHeaders.append("Content-Type", "application/json");
    json = JSON.stringify({"Hello": "World"});
    var zlibOpts = {
        level: 6,
        to: 'string'
    };
    data = pako.gzip(json);

    var requestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: data
    };

    fetch("https://<endpoint>", requestOptions)
    .then(response => response.text())
    .then(result => {
        console.log(result);
    })
    .catch(error => console.log('error', error));
0

There are 0 best solutions below