twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.19k stars 327 forks source link

Twirp on AWS APIG+Lambda #32

Closed rynop closed 6 years ago

rynop commented 6 years ago

Anyone tried to run Twirp on Lambda yet?

I'm not a fan of API Gateway (APIG) and all the complexity, but I love lambda. I think it would be sweet to do the following:

  1. Configure APIG to handle ANY method on the /{proxy+} resource will reverse proxy everything to one Lambda function (lambda function is a go binary using Twirp).
  2. HTTP 1.1 application/json request comes into APIG, it gets routed to Lambda
  3. Thin go handler code transforms the events. APIGatewayProxyRequest (github.com/aws/aws-lambda-go/events) to the HTTP request object that Twitch needs.
  4. Twitch router works as normal
  5. Thin layer that takes the Twirp response and transforms it to a return events.APIGatewayProxyResponse

Questions (I'm a go noob, never used RPC or protobuf but they make sense to me):

  1. Am I missing anything big here that would prevent me from doing this?
  2. Can anyone point me to the Twirp code that would get me started on 3 and 5 above?
rynop commented 6 years ago

https://github.com/apex/gateway looks to be promising, will investigate and report back

rynop commented 6 years ago

https://github.com/apex/gateway still requires a full blown HTTP server (listening on a port etc). I think I can get it to work, but my “ideal” solution would be to use all the Twirp badassness (routing, code generation, error handling etc) WITHOUT the need for the HTTP stack because APIG is already providing the HTTP stack - for example already parsed the headers out of HTTP, which apex/gateway has to re-do to send onto net/http.

Would it require a ton of Twirp re-factoring to “turn off” the http protocol stuff and instead just pass the Headers and POST body to Twirp directly? I’d just pass along the Twirp sterilized JSON response on the Lambda return.

Bummer is to get application/protobuf working I’d have to base64 encode the Twrip protobuf response before sending it back from Lambda (which incurs performance penalty)

rynop commented 6 years ago

@spenczar correctly pointed out to me that:

the address part of apex/gateway's ListenAndServe is a red herring - if you check the source, you'll see that it's unused. It's just there to match the standard library net/http.ListenAndServe signature that people are used to

So apex/gateway does not start a full blown HTTP server. Re-setting the HTTP headers is almost a no-op, a small price to pay to get integration with Twirp.

I will report back here if/when I get something working.

rynop commented 6 years ago

I got this working. It is super slick. I'll write a blog post and put link here in the next day or so.

rynop commented 6 years ago

OK wrote a blog post and created an example/guide that details how to run Twirp completely serverless on AWS with JSON and binary protobuf support.

Closing