Skip to content

Overwriting read-only Lambda@Edge headers #262

@UnderSampled

Description

@UnderSampled

Hooking up a Mangum lambda to CloudFront as EventType: origin-request returns a 502 response: "The Lambda function result failed validation: The function tried to add, delete, or change a read-only header."

According to the documentation, the Content-Length Header is one of the read-only headers for origin-request events. Not quite sure why. But it's certainly one of the headers returned when calling the lambda. I use the lambda to handle API requests, so it needs IncludeBody, which is only available with origin-request.

I was able to get around this by hijacking the response:

def handler(event, context):
    response = Mangum(app, lifespan="off")(event, context)
    if 'headers' in response:
        response['headers'].pop('content-length')
    return response

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededimprovementImprove an existing feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions