I have a lambda target behind an ALB. My lambda is a python lambda.

def handler(event, context):
response = {
    "statusCode": 200,
    "statusDescription": "200 OK",
    "isBase64Encoded": False,
    "headers": {
        "Content-Type": "text/html; charset=utf-8"
    }
}

On hitting my url using curl though, I receive a

< HTTP/1.1 200 OK
< Server: awselb/2.0
< Date: Sat, 06 Apr 2019 04:46:50 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive

Note Content-Type is an octet-stream, which causes browsers to download the response as a file instead of displaying it. I tried adding additional headers "Foo":"Bar" to the response and they don't show up in the curl response. ALB seems to be eating my lambda supplied headers. How can I fix this?

2 Answers

0
RaGe On Best Solutions

Turns out I had multivalue headers turned on for my target group. With that setting turned on, my lambdas needs to return a response with field multiValueHeaders set instead of headers. So my lambda code needed to be:

def handler(event, context):
response = {
    "statusCode": 200,
    "statusDescription": "200 OK",
    "isBase64Encoded": False,
    "multiValueHeaders": {
        ["Content-Type": "text/html; charset=utf-8"]
    }
}

More information here.

0
Community On

AWS Application Load Balancer transforms all response headers to lowercase, you need to check your headers carefully. Unfortunately, you can not change or modify the headers are manipulated by the ALB. You can refer below link for HTTP Headers:

https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html

For Request Tracing for Your Application Load Balancer :

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html

Also, you need to check the limitations for the Load Balancer:

  • The load balancer updates the header when it receives an incoming request, not when it receives a response.

  • If the HTTP headers are greater than 7 KB, the load balancer rewrites the X-Amzn-Trace-Id header with a Root field.

  • With WebSockets, you can trace only until the upgrade request is successful.