3
votes

I'm uploading an image to s3, through a lambda, and everything works well, with no errors, but the response from API Gateway is 500 Internal Server Error.

I configured my api-gateway following this tutorial: Binary Support for API Integrations with Amazon API Gateway.

My lambda receives the base64Image, decode it and successfully upload to s3.

This is my lambda code:

def upload_image(event, context):
    s3 = boto3.client('s3')
    b64_image = event['base64Image']
    image = base64.b64decode(b64_image)

    try:
        with io.BytesIO(image) as buffer_image:
            buffer_image.seek(0)
            s3.upload_fileobj(buffer_image, 'MY-BUCKET', 'image')

        return {'status': True}

    except ClientError as e:
        return {'status': False, 'error': repr(e)}

This is what i'm receiving: { "message": "Internal server error" }, with a 500 status code.

Obs: I'm not using lambda proxy integration.

2
How are you importing boto3? Do you have boto3 included in your project environment?WillardSolutions
Yes, i have boto3 included in my project and i already tested my lambda isolated, it is working well.Daniel Wagner

2 Answers

2
votes

You need to return a header in the response, e.g. in Python:

    return {
        "statusCode": 200,
        'headers': { 'Content-Type': 'application/json' },
        "body": json.dumps(body)
    }
0
votes

That example looks like it falls short on mapping the responses section in favor of a pass through. In which case changing your return to: return {'status': True, 'statusCode': 200} might work.

Generally speaking there are two paths when building a response with ApiGateway-Lambda. One is the lambda-proxy (where your lambda function defines the response), the other is where ApiGateway transforms your responses and generates the appropriate headers/status based on a mapping.

The path from the example is for the latter.

Personally I would change: return {'status': True} to return {'status': "Success"} And create a regex that looks for the word "Success" and "Error" respectively.

I have used this blog post successfully with this technique (it also describes at length the differences between the two approaches). Once you get one mapping working you could adjust it as is more appropriate for your implementation.

EDIT: hot tip these decorators are awesome and make python & lambda even cleaner/easier but mostly for the proxy setup