awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.03k stars 198 forks source link

issue-144 Using aws-lambda-go-api-proxy to enable echo Framework and Lambda with ALB #146

Open ikegam1 opened 1 year ago

ikegam1 commented 1 year ago

Issue #144

Description of changes: I used aws-lambda-go-api-proxy to use echo Framework and Lambda with ALB. However, it failed because the response type from Lambda to ALB is different. To support this, I made an adapter for echo.

The base is the code of the api gateway.

スクリーンショット 2022-09-18 16 42 47

Details

ALB event format

https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

example (code fragment)

e := echo.New()
echoLambda := echoadapter.NewALB(e)

func handler(ctx context.Context, req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
        return echoLambda.ProxyWithContext(ctx, req)
}

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

An example using sam can be found here

go test result

••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Ran 96 of 96 Specs in 0.002 seconds
SUCCESS! -- 96 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok      github.com/awslabs/aws-lambda-go-api-proxy/core 0.349s

•
Ran 3 of 3 Specs in 0.001 seconds
SUCCESS! -- 3 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok      github.com/awslabs/aws-lambda-go-api-proxy/echo 0.237s

debug result

request & response

% curl -XPOST http://xxxxxxxx.ap-northeast-1.elb.amazonaws.com/t/ -d '{"msg":"aaa"}' -H "application/json"
"hello world!"

cloud watch logs(debug)

START RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5 Version: $LATEST
--
2022/09/18 06:31:47 req: events.ALBTargetGroupRequest{HTTPMethod:"POST", Path:"/t/", QueryStringParameters:map[string]string{}, MultiValueQueryStringParameters:map[string][]string(nil), Headers:map[string]string{"accept":"*/*", "content-length":"13", "content-type":"application/x-www-form-urlencoded", "host":"xxxxxxxx.elb.amazonaws.com", "user-agent":"curl/7.64.1", "x-amzn-trace-id":"Root=1-6326bb53-576053df0f5f6f8c40fa7b23", "x-forwarded-for":"000.000.000.0000", "x-forwarded-port":"80", "x-forwarded-proto":"http"}, MultiValueHeaders:map[string][]string(nil), RequestContext:events.ALBTargetGroupRequestContext{ELB:events.ELBContext{TargetGroupArn:"arn:aws:elasticloadbalancing:ap-northeast-1:00000000:targetgroup/xxxxxxxx/xxxxxx"}}, IsBase64Encoded:true, Body:"eyJtc2ciOiJhYWEifQ=="}
2022/09/18 06:31:47 routing t
2022/09/18 06:31:47 reqBody: "{\"msg\":\"aaa\"}"
{     "time": "2022-09-18T06:31:47.559549149Z",     "id": "",     "remote_ip": "000.000.000.000",     "host": "",     "method": "POST",     "uri": "/t",     "user_agent": "curl/7.64.1",     "status": 200,     "error": "",     "latency": 26490,     "latency_human": "26.49µs",     "bytes_in": 13,     "bytes_out": 15 }
END RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5
REPORT RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5  Duration: 1.25 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 33 MB

Please confirm.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

0angelic0 commented 1 year ago

Hello, any chance that this PR will get a look.

0angelic0 commented 1 year ago

I tried merge this PR into my fork and it is working very well. And I even successfully added fiberadapter for ALB on top of this PR. It could be great if we can get this PR merged into this main awslabs repo.