aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Returning compressed(gzip) json from API running inside of Amazon.Lambda.AspNetCoreServer

Open StewartSnow opened this issue 7 years ago • 4 comments

Hi There, Related to this issue - https://github.com/aws/aws-lambda-dotnet/issues/255 except in the context of a full .net web api running within the ASPNetCoreServer proxy layer.

I can't seem to get it working, either by:

  • Using .Net's compression package ("Microsoft.AspNetCore.ResponseCompression") - so that the API itself compresses the response (which I think is likely to then confuse the proxy layer in terms of how to return it through to API gateway.

OR

  • Using the compression options available on API Gateway (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-receive-response-with-compressed-payload.html) - however I believe these again are being foxed because the option is designed to work with traditional native lambda function, rather than via a proxy.

Using the first option locally via localhost works fine, however doing so through Lambda / API Gateway results in no compression at all. I've tried tricking the project into thinking that the application/json type responses are binary data, rather than plain text to skip out any post-processing that's going on - but with no apparent success: RegisterResponseContentEncodingForContentType("application/json", ResponseContentEncoding.Base64);

Has anyone managed to get json responses from a web-api project running in Lambda / API Gateway successfully compressed with gzip or similar? It would be fantastic if we could - as it would notably improve the performance of larger response payloads.

Many Thanks in advance!

StewartSnow avatar Dec 06 '18 09:12 StewartSnow

I have this working! There's a couple things you need to do.

First, because the API Gateway uses SSL by default, you need to tell the .Net Core middleware that it is ok to compress HTTPS responses:

In Startup.cs:

            services.AddResponseCompression(options =>
            {
                options.Providers.Add<GzipCompressionProvider>();
                options.MimeTypes =
                    ResponseCompressionDefaults.MimeTypes;
                options.EnableForHttps = true;
            });

Second, after you do this, the APIGatewayProxyFunction will garble your responses, because the Content-Type is still application/json. I looked at the source for marshalling the response - the problem is it only looks at content type, but not content encoding (<---- @klaytaybai take note, this could be a solution!) Therefore, the code does not recognize the response is compressed, and serializes it incorrectly.

So, next, you need to include the following hack in LambdaEntryPoint.cs to make it leave json alone.

        protected override void Init(IWebHostBuilder builder)
        {
            builder
                .UseStartup<Startup>();

            //so that lambda won't mangle gzip'd responses
            RegisterResponseContentEncodingForContentType("application/json",
                    ResponseContentEncoding.Base64);
        }

This will tell the API this is binary content that should not be manipulated.

Finally, if you haven't already, add application/* to your API Gateway's list of allowed binary media types under "Settings". You need to re-deploy the API Gateway stage for this to take effect.

This is currently working for me.

A final note: It was necessary for me to do compression at the API level instead of letting the Gateway do it, because my application had some large response objects. These objects were large enough (around 4mb) that they overflowed the buffer when the proxy function tried to marshall the request. Therefore, I had to shrink the response body before the proxy tried to read it.

adamrkerr avatar Feb 20 '19 18:02 adamrkerr

I determined that the mime types you specify in the AddResponseCompression must match exactly the mime types that you specify with RegisterResponseContentEncodingForContentType. For example, if you had a home page or swagger page that returned text/plain, it would fail in Lambda if you did not also specify this as base64 encoded. In my case, I specified only the application/json for options.MimeTypes . You should also be able to specify all of ResponseCompressionDefaults.MimeTypes as base64 encoded if prefer to have all the mime types compressed.

ronl avatar Mar 04 '19 13:03 ronl

Hi @StewartSnow,

Good afternoon.

I was goin through the issue backlog and came across this guidance question. Please confirm if guidance provided by @adamrkerr works for you and if this issue could be closed.

Thanks, Ashish

ashishdhingra avatar Sep 11 '20 21:09 ashishdhingra

Hi @StewartSnow,

Good afternoon.

I was goin through the issue backlog and came across this guidance question. Please confirm if guidance provided by @adamrkerr works for you and if this issue could be closed.

Thanks,

Ashish

Yes problem was resolved via the above. Good to close

StewartSnow avatar Sep 11 '20 22:09 StewartSnow