Go Lambda 的 AWS ALB 总是返回"502 Bad Gateway"



我有一个用Go lang实现的AWS Lambda。Lambda由ALB触发。当我从外部调用ALB时,它总是返回以下信息:

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

在CloudWatch中,我可以看到Lambda被调用了。在这篇文章中,我读到ALB期望Lambda提供一个非常具体的响应对象。我已经将其实现为一个结构。这是Go Lambda代码:

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 )
}

在Cloudwatch中,我可以看到Lambda被调用,这是它返回的字符串:

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

据我所知,它看起来像本文中描述的响应规范。

ALB本身的日志如下所示:

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" "-" "-"

我已经花了几个小时进行调试,但我真的不知道为什么ALB总是返回502错误。你能看到错误吗?我做错了什么?

通过注释中的调试解决:您需要从处理程序返回实际的Response结构,而不是包含JSON的字符串。lambda库自行处理将返回值序列化为JSON的操作。

最新更新