The following solution allowed me to serve favicon via API gateway + Lambda with creating or using other resources.
The API used is a REST API and the application I am running on my lambda is a Plotly Dash application.
For the deployment of my API I set the following in template.yaml
(fyi: below is not the complete template)
Resources:
DashApi:
Type: AWS::Serverless::Api
Properties:
BinaryMediaTypes:
- "image/*"
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Events:
DashEndpoint:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
RestApiId:
Ref: DashApi
The important thing here is to set BinaryMediaTypes
because this will tell your API which data to pass as binary data and which to pass as utf-8 encoded data. In this example I set all image types as binary, but you also set all content types as binary with "*/*"
, or list specific image types only.
Perhaps for many web servers this is already enough, however for Plotly Dash applications you have to set another setting. For those applications I use the aws-wsgi package for handling the response. The call in this library will decode everything as utf-8 by default, unless it's told otherwise specifically. For this you have to set the base64_content_types
input parameter (list of strings).
Note: the asterix wildcard doesn't work here since awsgi
does a literal comparison of Content-Type
with all entries in the list
def lambda_handler(event, context):
base64_content_types = ['image/vnd.microsoft.icon', 'image/x-icon']
return awsgi.response(app.server, event, context, base64_content_types )
With these two settings set, I got favicon to load.
I hope this information still helps somebody. I list it here anyways because there is very little to be found on the web on solving this issues without using an S3 bucket.