amazon-web-servicesgoaws-lambdaaws-alb

AWS ALB of a Go Lambda always returns "502 Bad Gateway"


I have an AWS Lambda implemented with Go lang. The Lambda is triggered by an ALB. When I invoke the ALB from outside it always returns this:

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>

In CloudWatch I can see that the Lambda was invoked. In this article I have read that the ALB expects a very specific response object from the Lambda. I have implemented that as a struct. Here is the Go Lambda code:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/aws/aws-lambda-go/lambda"
    "log"
)

type Request struct {
    HttpMethod string `json:"httpMethod"`
    Path string `json:"path"`
    QueryStringParameters map[string]string `json:"queryStringParameters"`
    IsBase64Encoded bool `json:"isBase64Encoded"`
    Body string `json:"body"`
    Headers RequestHeaders `json:"headers"`
}

type RequestHeaders struct {
    Accept string `json:"accept"`
    AcceptLanguage string `json:"accept-language"`
    ContentType string `json:"Content-Type"`
    Cookie string `json:"cookie"`
    Host string `json:"host"`
    UserAgent string `json:"user-agent"`
    XAmznTraceId string `json:"x-amzn-trace-id"`
    XForwardedFor string `json:"x-forwarded-for"`
    XForwardedPort string `json:"x-forwarded-port"`
    XForwardedProto string `json:"x-forwarded-proto"`
}

type Response struct {
    IsBase64Encoded bool `json:"isBase64Encoded"`
    StatusCode int `json:"statusCode"`
    StatusDescription string `json:"statusDescription"`
    Headers *ResponseHeaders `json:"headers"`
    Body string `json:"body"`
}

type ResponseHeaders struct {
    ContentType string `json:"Content-Type"`
}

func HandleRequest(ctx context.Context, request Request) (string, error) {
    fmt.Println("Hello " + request.Body)
    responseHeaders := new(ResponseHeaders)
    responseHeaders.ContentType = "application/json"
    response := new(Response)
    response.IsBase64Encoded = false
    response.StatusCode = 200
    response.StatusDescription = "200 OK"
    response.Headers = responseHeaders
    response.Body = "{\"hello\":\"world\"}"
    json, err := json.Marshal(response)
    if err != nil {
        log.Fatal(err)
    }
    responseString := string(json)
    log.Println(responseString)
    return responseString, nil
}

func main() {
    lambda.Start( HandleRequest )
}

In Cloudwatch I can see that the Lambda is invoked and this the string it returns:

{
    "isBase64Encoded": false,
    "statusCode": 200,
    "statusDescription": "200 OK",
    "headers": {
        "Content-Type": "application/json"
    },
    "body": "{\"hello\":\"world\"}"
}

As far as I can tell it looks like the described response specification in this article.

The logs from the ALB itself look like this:

http 2020-07-13T11:49:51.014327Z app/test-Lambda/3e92b31e6a921454 176.199.208.26:54486 - 0.006 0.021 -1 502 - 736 293 "POST http://test-lambda-999999999.eu-central-1.elb.amazonaws.com:80/ HTTP/1.1" "insomnia/7.1.1" - - arn:aws:elasticloadbalancing:eu-central-1:999999999:targetgroup/test-lambda-target/540454d9390da765 "Root=1-5f0c4a5e-ca4e4a43b6c48633dc4c5b3e" "-" "-" 0 2020-07-13T11:49:50.986000Z "forward" "-" "LambdaInvalidResponse" "-" "-"

I invested already a couple of hours in debugging but I really don't know why the ALB always returns a 502 error. Can you see the error? What I'm doing wrong?


Solution

  • Solved via debugging in comments: you need to return your actual Response structure from the handler, not a string containing JSON. The lambda library handles serializing the return value to JSON on its own.