Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ALB returning 502 from Lambda instead of custom HTTP status

I've created a Lambda that check on a DynamoDB table the existence of a record matching host and request path and, if found, returns a redirect to the matching URL.

My Lambda returns this response but the ALB returns 502.

{
    "statusCode": 301,
    "statusDescription": null,
    "headers": {
        "Location": "https://www.my-target.co.uk/"
    },
    "multiValueHeaders": null,
    "body": "Redirecting to https://www.my-target.co.uk/",
    "isBase64Encoded": false
}

This is the log I find in CloudWatch for the Lambda function

START RequestId: 8b5a28f2-c56d-4418-a7b9-66ebe0ba2470 Version: $LATEST
[Information] EMG.ApplicationLoadBalancerRequestHandler: Received: GET / 
[Information] EMG.ApplicationLoadBalancerRequestHandler: Processing: my-test.net / 
[Information] EMG.RedirectManagers.RedirectTableRedirectManager: Fetching item: my-test.net / from table redirect-table 
[Information] EMG.ApplicationLoadBalancerRequestHandler: Found: https://www.target.co.uk/ Permanent 
END RequestId: 8b5a28f2-c56d-4418-a7b9-66ebe0ba2470
REPORT RequestId: 8b5a28f2-c56d-4418-a7b9-66ebe0ba2470  Duration: 69.59 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 39 MB  

This is the response I get

HTTP/1.1 502
status: 502
Server: awselb/2.0
Date: Thu, 15 Aug 2019 19:13:58 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
<html>
 <head><title>502 Bad Gateway</title></head>
 <body bgcolor="white">
 <center><h1>502 Bad Gateway</h1></center>
 </body>
 </html>

I couldn't find anything saying we can't return non-200 responses from Lambda, so I really have no idea...

You can also find this question in the relevant GitHub repo: https://github.com/aws/aws-lambda-dotnet/issues/507

like image 946
Kralizek Avatar asked Aug 19 '19 18:08

Kralizek


People also ask

Why am I getting an HTTP 502 Error with my Lambda?

If you’re using Lambda@Edge, an HTTP 502 status code can indicate that your Lambda function response was incorrectly formed or included invalid content. For more information about troubleshooting Lambda@Edge errors, see Testing and debugging Lambda@Edge functions .

What is a 502 Error message in AWS CloudWatch?

Example HTTP 502 "Malformed Lambda proxy response" error message as it appears in Amazon CloudWatch Logs Thu Dec 08 01:13:00 UTC 2016 : Execution failed due to configuration error: Malformed Lambda proxy response Thu Dec 08 01:13:00 UTC 2016 : Method completed with status: 502

How do I use AWS Lambda with load balancer?

Using AWS Lambda with an Application Load Balancer You can use a Lambda function to process requests from an Application Load Balancer. Elastic Load Balancing supports Lambda functions as a target for an Application Load Balancer.

What is AWS Lambda developer guide?

AWSDocumentationAWS LambdaDeveloper Guide Using AWS Lambda with an Application Load Balancer You can use a Lambda function to process requests from an Application Load Balancer. Elastic Load Balancing supports Lambda functions as a target for an Application Load Balancer.


1 Answers

Apparently I was missing a required property of the returned HTTP response.

Here is the relevant part of the AWS documentation (emphasis mine)

The response from your Lambda function must include the Base64 encoding status, status code, status description, and headers. You can omit the body. The statusDescription header must contain the status code and reason phrase, separated by a single space.

Changing my code so that the response of the Lambda complied to the requirement fixed the issue.

{
    "statusCode": 301,
    "statusDescription": "301 Found",
    "headers": {
        "Location": "https://www.my-target.co.uk/"
    },
    "multiValueHeaders": null,
    "body": "Redirecting to https://www.my-target.co.uk/",
    "isBase64Encoded": false
}
like image 197
Kralizek Avatar answered Oct 13 '22 21:10

Kralizek