Gzip compression with AWS Lambda deployed applications

Ok, problem solved! As usual, the problem I encountered was due to my own oversights rather than something wrong happening in Dash. (I forgot to add a return statement to lambda_handler, therefore AWS was not receiving any response which resulted in the internal server error). Though I have learned about how compression works in Dash application. I will summarize the lessons I learned here for future reference in case anyone want to use compression in there Dash apps that run on AWS:

  1. Enable compression in your Dash instance: app = dash.Dash(compress=True, ...)
  2. If you are using an API resource for your AWS application, in your tools’ template.yaml set "*/*" under Properties -> BinaryMediaTypes to tell your API that everything will be passed as binary data and it should not block that data.
  3. Wherever you call awsgi.response(), create a list of all Content-Type that will be communicated to and from the server. And pass that list to the base64_content_types parameter of awsgi.response().
    a. You have to be explicit in your Content-Type list as awsgi does not expand wildcard characters, i.e. you cannot just have "*/*" in the list because awsgi will do a literal comparison between the Content-Type of the communication and what is in the base64_content_types list.
    b. If you see UnicodeDecodeErrors in your CloudWatch logs coming from awsgi.__init__ then this most likely indicates that you encountered a Content-Type that you didn’t add to the list yet. If you can’t figure out what the correct type is (the Content-Type that awsgi sees is not always visible in the incoming event data), then do the following. Build your tool locally, then in the build folder, go to awsgi/__init__.py and add a print statement in the use_binary_response() function that prints the Content-Type to the AWS logs. Then deploy this adjusted version and test it out.

That’s it, the Dash code will take care of the compression/decompression by itself, just as you would expect.

1 Like