Previously, AWS API Gateway did not support binary responses, making it impossible to return images from your serverless API. Now they do (see https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/). We need to be able to configure HTTP endpoints/events in serverless to use this new functionality.
Does it mean we will be able to gzip response?
My team is eagerly anticipating this feature in Serverless. We have an image resizing service that currently needs to proxy responses through a traditional server to return images... 😫
Does anybody know of a workaround we could use until this is added to Serverless?
@vladgolubev our image resizing service resizes the images on-demand based on query string params. The lambda first checks if the requested image size already exists on S3. If it does, it returns the existing image, and if not it generates it, stores it on S3 and then returns it. So direct S3 links won't work for us.
Our current workaround is to run a simple Node.js reverse proxy that calls the Lambda image resize service, gets the direct S3 link, and then returns the binary response to the client...
But this is a temporary solution. When Serverless adds binary response support (or if we can find a workaround, like making changes directly in AWS after deploying with Serverless) we can get rid of the reverse proxy without changing any previously generated image URLs.
Note: there's some thread in the forum about this: http://forum.serverless.com/t/returning-binary-data-jpg-from-lambda-via-api-gateway/796
The current issue - as far as I know - is that the binaryMediaTypes cannot be set in CloudFormation.
So your only option right now is to configure them manually (if I remember correctly it survives a re-deployment).
See here: https://github.com/bbilger/jrestless-examples/tree/master/aws/gateway/aws-gateway-binary
or here: https://github.com/krisgholson/serverless-thumbnail
or in the blog post: https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/
and sure you can also gzip: https://github.com/bustlelabs/gziptest/ (not using serverless)
I would, however, not expect too much from it since
a) API Gateway has a size limit of 10MB (+ I guess you have to substract the base64 overhead from it)
b) only if a proper Accept header (for the response) or Content-Type header (for the request) i.e. one registered as binary media type is set, you'll get binary data, else you'll end up with base64 encoded content.
c) If I remember correctly there are issues with multiple accept headers like "Accept: image/png,image/gif"