yarbsemaj / sveltekit-adapter-lambda

An adapter to build a SvelteKit app into a lambda ready for deployment with lambda proxy via the Serverless Framework or CDK.
https://www.npmjs.com/package/@yarbsemaj/adapter-lambda
MIT License
77 stars 16 forks source link

Unnecessary ApiGateway #9

Closed ivands closed 2 years ago

ivands commented 2 years ago

I feel like your serverless.yaml file unnecessary uses ApiGateway, leading to unnecessary costs on scale. Would it be possible to let CloudFront call the lambda directly?

I found this forum post about it. https://forum.serverless.com/t/directly-proxying-lambda-via-cloudfront-without-api-gateway/3808/4

Or even better would be to host the whole svelte SSR lambda on the edge. But that would probably not work because of constraints on the edge.

ivands commented 2 years ago

AWS did at some point leak info about a new feature that would allow you to get a public-facing URL for your lambda. https://beabetterdev.com/2021/11/21/aws-releases-lambda-function-urls-finally/ But sadly they still haven't released this feature.

yarbsemaj commented 2 years ago

Hi @ivands, Interesting concept discussed in the thread you referenced. I wasn't aware this was possible outside of edge Lambdas, you learn something new every day 😄. Doing a bit more digging through that thread,unfortunately I hit a blocker with this approach...

Lambda can only return JSON. API Gateway can use transforms to change that JSON into other things. For example if a lambda returns HTML as a JSON result, API Gateway can strip the JSON and return an HTML page.

Iv tested this out and It does appear to be the case. I'm also unsure how you would do thing like set headers and cookies without API Gateway (and lambda proxy)

As Lambda@edge functions can't read the contents of a response body it's not possible to emulate this function at the edge.

ivands commented 2 years ago

That does seem to be a blocker. I guess some kind of transformation step is still needed. Sadly ApiGateway is kinda expensive for this use case.

I'm guessing we would probably need native CloudFront lambda origin support.

yarbsemaj commented 2 years ago

Hi @ivands I get that, To help mitigate this iv moved to using a HTTP API gateway this does give you more limited options(but for a public facing website I doubt this really matters) but the cost saving are huge, around 70% from what I have read. You can start using this now with v0.7.0. NB you will have to update your serverless.yml file

ivands commented 2 years ago

Hi @yarbsemaj, that's a great improvement. To eliminate API Gateway entirely we will just have to wait for native lambda support in CloudFront.