apollographql / router

A configurable, high-performance routing runtime for Apollo Federation 🚀
https://www.apollographql.com/docs/router/
Other
804 stars 272 forks source link

How to use this as AWS Lambda #364

Open pradeepkusingh opened 2 years ago

pradeepkusingh commented 2 years ago

I would like to deploy this as AWS Lambda serverless, please help any docs, example would be helpful.

medelman17 commented 2 years ago

Hi, @pradeepkusingh! Great minds think alike, eh? As luck would have it, I've been working on a reference architecture and write-up to demonstrate using the router in a serverless function. It's still not fully baked, but watch this space and I'll make sure to ping you when it's ready.

Cheers!

savnik commented 2 years ago

We will also start experimenting with this so if you have any WIP we would love to see it/review and give feedback

pradeepkusingh commented 2 years ago

@medelman17 Have you had a chance to make this API AWS Lambda compliant?

o0Ignition0o commented 2 years ago

Hi @pradeepkusingh,

I'm bumping this (cc @medelman17 ) because a recently opened PR shows how to build your own tower::Service compliant router, which might play nice with the AWS services, expecially AWS lambda runtime and the AWS sdk which both seem to be tower based!

hrkfdn commented 2 years ago

Hey @o0Ignition0o, thanks for the heads up. I actually wanted to give this a try but it doesn't seem that simple (or I'm missing something). So the apollo-router itself can not be fed with HTTP requests from other sources, thus a router needs to be created using PluggableRouterServiceBuilder, which means automatic schema reloading needs to be reimplemented, but that's not a huge problem of course.

The other issue is in cross-compilation: Apollo Router uses system libs (i.e. OpenSSL) via reqwest, which makes linking a little harder as the Amazon Linux 2 used in Lambdas is somewhat old. It would be a little easier if reqwest was used with the rustls-tls feature enabled, but I'm not sure if that's an option for you and it's not an insignificant change.

At this point I put the project on pause. Maybe I'll do some more investigation once I have the time. A bit of a shame, because it wasn't far away from a prototype and I really would have loved to try out the awesome work on the Rust router with AWS Lambda 😄

adikari commented 2 years ago

any updates on this? I am currently using the nodejs version of the gateway in AWS lambda and want to start using the apollo router instead.

pradeepkusingh commented 1 year ago

Any update on this requests?

jaypea commented 1 year ago

has anyone tried a custom docker image for lambda by adding the lambda-ric wrapper around the existing docker image?

bradens commented 1 year ago

has anyone tried a custom docker image for lambda by adding the lambda-ric wrapper around the existing docker image?

I don't think this works as the docker container boots an http server right?

Tehnix commented 12 months ago

This seems completely blocked on either:

I did a bit of experimenting:

I made a proof-of-concept here for the curious. Here's a router that spins up the binary and proxies Lambda Events to it locally here along with CDK deployment creating a layer with the binary and a lambda with the handler code here.

I've validated the idea locally, and as far as Lambda would take it before running into GLIBC issues, using cargo-lambda to invoke the handler that is spinning up the router binary.

As I note in the README of ms-router, it will run into the following during runtime:

/lib64/libm.so.6: version `GLIBC_2.27' not found
/lib64/libm.so.6: version `GLIBC_2.29' not found
/lib64/libc.so.6: version `GLIBC_2.28' not found

And then ultimately fail. Currently, only option is to use the JavaScript based @apollo/gateway which is a shame :/

Tehnix commented 11 months ago

I gave it another shot, this time by pulling in apollo-router as a library, and then building it myself. A bit of a similar method as above where I let the Router start its HTTP server and proxy Lambda Events to it, although this time a bit more clean since it's all kept within the same Rust program (no external binary).

You can see how exactly that is set up in this repo here https://github.com/codetalkio/the-stack/tree/d68a638bfcff7fa035f636f3abb50d3ba14548b6/ms-apollo.

I currently get the following error at the very last step, which I assume is the linking:

$ cargo lambda build # uses cargo-zigbuild
   ...
   Compiling deno_crypto v0.129.0
   Compiling opentelemetry-proto v0.3.0
   Compiling opentelemetry-otlp v0.13.0
   Compiling tikv-jemallocator v0.5.4
   Compiling ms-apollo v0.1.0 (~/GitHub/codetalkio/the-real-stack/ms-apollo)
error: linking with `~/Library/Caches/cargo-zigbuild/0.17.3/zigcc-x86_64-unknown-linux-gnu.sh` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="~/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin
  ...huge section of paths...
-gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-Wl,--strip-all" "-nodefaultlibs"
  = note: ld.lld: error: undefined symbol: __start_linkm2_PLUGINS
          >>> referenced by apollo_router.c57000e1db575776-cgu.00
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.00.rcgu.o:(apollo_router::configuration::_::_$LT$impl$u20$schemars..JsonSchema$u20$for$u20$apollo_router..configuration..Configuration$GT$::json_schema::heb9047cee3a0bda9) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.08
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.08.rcgu.o:(apollo_router::router_factory::create_plugins::_$u7b$$u7b$closure$u7d$$u7d$::h10977a494c764e3e) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.11
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.11.rcgu.o:(schemars::gen::SchemaGenerator::json_schema_internal::h88869beee2a5a5e8) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced 3 more times
          >>> the encapsulation symbol needs to be retained under --gc-sections properly; consider -z nostart-stop-gc (see https://lld.llvm.org/ELF/start-stop-gc)

          ld.lld: error: undefined symbol: __stop_linkm2_PLUGINS
          >>> referenced by apollo_router.c57000e1db575776-cgu.00
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.00.rcgu.o:(apollo_router::configuration::_::_$LT$impl$u20$schemars..JsonSchema$u20$for$u20$apollo_router..configuration..Configuration$GT$::json_schema::heb9047cee3a0bda9) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.08
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.08.rcgu.o:(apollo_router::router_factory::create_plugins::_$u7b$$u7b$closure$u7d$$u7d$::h10977a494c764e3e) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.11
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.11.rcgu.o:(schemars::gen::SchemaGenerator::json_schema_internal::h88869beee2a5a5e8) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced 3 more times

          ld.lld: error: undefined symbol: __start_linkme_PLUGINS
          >>> referenced by apollo_router.c57000e1db575776-cgu.00
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.00.rcgu.o:(apollo_router::configuration::_::_$LT$impl$u20$schemars..JsonSchema$u20$for$u20$apollo_router..configuration..Configuration$GT$::json_schema::heb9047cee3a0bda9) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.08
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.08.rcgu.o:(apollo_router::router_factory::create_plugins::_$u7b$$u7b$closure$u7d$$u7d$::h10977a494c764e3e) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.08
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.08.rcgu.o:(apollo_router::router_factory::create_plugins::_$u7b$$u7b$closure$u7d$$u7d$::h10977a494c764e3e) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced 5 more times
          >>> the encapsulation symbol needs to be retained under --gc-sections properly; consider -z nostart-stop-gc (see https://lld.llvm.org/ELF/start-stop-gc)

          ld.lld: error: undefined symbol: __stop_linkme_PLUGINS
          >>> referenced by apollo_router.c57000e1db575776-cgu.00
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.00.rcgu.o:(apollo_router::configuration::_::_$LT$impl$u20$schemars..JsonSchema$u20$for$u20$apollo_router..configuration..Configuration$GT$::json_schema::heb9047cee3a0bda9) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.08
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.08.rcgu.o:(apollo_router::router_factory::create_plugins::_$u7b$$u7b$closure$u7d$$u7d$::h10977a494c764e3e) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced by apollo_router.c57000e1db575776-cgu.11
          >>>               apollo_router-4b09e49663ee2f23.apollo_router.c57000e1db575776-cgu.11.rcgu.o:(schemars::gen::SchemaGenerator::json_schema_internal::h88869beee2a5a5e8) in archive ~/GitHub/codetalkio/the-real-stack/ms-apollo/target/x86_64-unknown-linux-gnu/release/deps/libapollo_router-4b09e49663ee2f23.rlib
          >>> referenced 3 more times

error: could not compile `ms-apollo` (bin "ms-apollo") due to previous error

Way out of my depth here, but if anyone has any suggestions on where to look/dig into, then I can see if I can take it further 😊

Is this the plugins functionality from Apollo? Would there be a way to disable it looking for those perhaps?

Tehnix commented 11 months ago

I couldn't let this go: Here's a proof-of-concept repo https://github.com/codetalkio/apollo-router-lambda.

The PoC builds the Router in an Amazon Linux 2 container so that we can actually run it in the Lambda runtime environment, and wraps it up in cargo-lambda.

TL;DR:

Any PRs with improvements or ideas are welcome 😀