Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALB is not propagating response headers correctly

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?

like image 966
RaGe Avatar asked Apr 06 '19 04:04

RaGe


People also ask

Does ALB strip headers?

But the ALB seems to strip the header and replace it with its own (which becomes X-Forwarded-Proto: http ), and then the backend application on the ECS servers sees http and writes all it's links/resource paths as http, causing an insecure mixed content warning in Safari, Chrome, etc.

How do I pass headers in API gateway test?

To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.

Can ALB add header?

Yes, It is possible to set CustomHeaders in Property Manager and reference this header in ALB within Cloudlets configuration.


1 Answers

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 on AWS' release blog post.

like image 114
RaGe Avatar answered Nov 12 '22 07:11

RaGe