How to return binary data from lambda function in AWS in Python?

I finally figured this out. Returning binary data from a python lambda is doable.

Follow the instructions here: https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/

Be sure to check the 'Use Lambda Proxy integration' when creating a new method.

Also be sure your Python Lambda response returns a base64-encoded body, sets isBase64Encoded to True, and an appropriate content type:

import base64

def lambda_handler(event, context):
    # ...

    body = base64.b64encode(bin_data)

    return {'isBase64Encoded'   : True,
            'statusCode'        : 200,
            'headers'           : { 'Content-Type': content_type },
            'body'              : body }

THEN:

For each of your routes/methods issue:

apigateway update-integration-response --rest-api-id <api-id> --resource-id <res-id> --http-method POST --status-code 200 --patch-operations "[{\"op\" : \"replace\", \"path\" : \"/contentHandling\", \"value\" : \"CONVERT_TO_BINARY\"}]"

In the AWS console. The and can be seen in the API Gateway 'breadcrumbs' ex:

<api-id> = zdb7jsoey8
<res-id> = zy2b5g

THEN: You need to 'Deploy API'. From what I found only it only worked AFTER deploying the API.

Be sure you setup the 'Binary Media Types' before deploying.

Hint: Nice AWS shell terminal here: https://github.com/awslabs/aws-shell

pip install aws-shell

Following all the steps above didn't work on my case, because having the binary support for content-type = */* will convert all responses to binary.

My case:

  • Multiple lambda functions returning json (text), just a single lambda returning a binary file. All have lambda proxy enabled.

  • The lambdas are in an API Gateway

  • The API Gateway is behind CloudFront

Hint: I have notice an important information in the API Gateway -> Settings

Binary support description

Quoting:

API Gateway will look at the Content-Type and Accept HTTP headers to decide how to handle the body.

This means that the Content-Type response header must match Accept request header

Solution:

  1. Set Binary Media Types in API gateway to your mime type: image/jpg

  2. In your HTTP request set Accept: image/jpg

  3. In your HTTP response set Content-Type: image/jpg

{
  "isBase64Encoded": True,
  "statusCode": 200,
  "headers": { "content-type": "image/jpg"},
  "body":  base64.b64encode(content_bytes).decode("utf-8")
}
  1. Next we must tell CloudFront to accept the 'Accept' header from the request. So, in CloudFront distribution, click on your API Gateway instance (ID is clickable) and once redirected to CloudFront instance go to Behaviour tab, select the path-pattern of your API (example: /api/*) and click on Edit button.

Example of path patterns

On the new screen, you have to add Accept header to Whitelist.

whitelist Accept

Note 1: If you have multiple file types, you must add them all to Binary Media Types in the API gateway settings

Note 2: For those coming from serverless and want to set the binary types when deploying your lambdas, then check this post: setting binary media types for API gateway

plugins:
  - serverless-apigw-binary

custom:
  apigwBinary:
    types:
- 'image/jpeg'

The serverless.yml file for cloudfront should contain:

resources:
    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        DistributionConfig:
          ...
          CacheBehaviors:
            ...
            - 
              #API calls
              ...
              ForwardedValues:
                ...
                Headers:
                  - Authorization
                  - Accept

As far as I can tell, this is also the case with Python 3. I'm trying to return a binary data (bytes). It's not working at all.

I also tried to use base-64 encoding and I have had no success.

This is with API Gateway and Proxy Integration.

[update]

I finally realized how to do this. I enabled binary support for type */* and then returned this:

return({
        "isBase64Encoded": True,
        "statusCode": 200,
        "headers": {
                "content-type": "image/jpg",
        },  
        'body':  base64.b64encode(open('image.jpg', 'rb').read()).decode('utf-8')
})