Today I learned how to deploy my own serverless function with a heavy caching layer using AWS Lambda (with a Layer for large dependency reuse), API Gateway, and CloudFront.
I am an enterprise developer now.
51 replies
-
-
-
@cthos where is my merit badge
@zachleat what’s a good fax number to reach 11ty’s sales engineers?
@ardouglass all questions must now flow through jira
@zachleat you too can charge $2000 consulting rates just like Thoughtworks
@benschwarz they charge *what* 😬
look—did I yolo this to production in the same day that I learned how to do it? yes, yes I did
vercel rewrite -> cloudfront -> api gateway -> aws lambda function is running the 11ty screenshots API now 👀
-
@jamesdoc this is the experiment! I’m going to blog all of this after it’s been about two weeks with some more usage data to compare
@jamesdoc took a little peek into the data early: https://fediverse.zachleat.com/@zachleat/112899549153465977
-
@d for a few weeks now, yeah.
Preliminary comparisons with Netlify, Vercel, and AWS Lambda+Cloudfront suggest that Vercel is *drastically* overcounting serverless invocation amounts. I suppose it’s possible that I misconfigured something but Netlify was like 4h/day, Vercel was 42h/day and this new AWS setup is looking like it’s going to weigh in at 2h/day?
Alarm bells are going off over here.
@zachleat Lambda is priced per millisecond, it sounds like Vercel rounds to the nearest 50ms? That might explain some of the difference. Also maybe the Lambda CPU is faster?
@williamoconnell oh that good to know. That’s a good distinction yeah, I believe the Vercel one is using 1769 MB of memory (which is how they scale CPU usage too) https://vercel.com/docs/functions/configuring-functions/memory and my Lambda is set for 1024 MB
@zachleat this is why we built Begin in such a way that you can bring your own AWS credentials to the party.
@macdonst if you gotta pick one, it’s the safest bet for sure 👍🏻
@zachleat not to throw shade on anyone but those other companies really, really mark up their services.
My AWS bill this month was $0.86 CAD. it would have been $40-50 otherwise.
@macdonst amazing yeah—I understand why folks aren’t switching but if y’all have cracked the DX of AWS I’m excited to learn more
A few things here: Vercel’s Edge Network cache is segmented into 18 regions. Further, the cache eviction policy is “best-effort” and assets may be evicted before they’re stale: https://vercel.com/docs/edge-network/caching#limits
I suspect I was getting very few cache hits—not ideal at all for a screenshots service (or any resource intensive serverless function?). I wanted those images to request once and cache for as long as possible.
@zachleat you’ll have a lot more control over this via Cloudfront. I am very pleased watching this thread.
Is that a weird thing to say? Am I being weird? Oh well.
@cthos to be honest I feel like it’s weird that more folks aren’t complaining about this? seems like a very expensive difference in service offerings
@zachleat it’s the managed service trap with a free tier. It’s easy to set up, and they’re hoping to keep you there with inertia and sunk cost fallacy.
That said AWS also has some major footguns.
@cthos Yeah, there is plenty of technical wiggle room to explain the difference away but 10× metrics has me scratching my head
@zachleat oh no I think Vercel is charging a pretty solid margin. I don’t have any hard numbers though.
@cthos I guess my hunch is that it isn’t “overhead” in the classical sense of we’re charging you a reseller fee for convenience. I’d be fine with that. But what if the *usage* measurements are inflated?
-
@zachleat I started to walk through (in addition to what you said above) and I have a couple of wild speculations. They could also be doing a "keep warm" tactic where they invoke the function to keep it awake periodically to eliminate cold starts.
@cthos oh huh! How warm should the start be 😅
@zachleat Depends on how "critical path" the function is. AWS will keep a lambda function "warm" for a period of time until it stops getting requests and then it'll put it to sleep. It'll incur a performance penalty to wake it back up. How long that penalty is depends a lot on the function and the language the function is written in. It can be as long as a full second though. One common tactic is to just have a cron job go ping the service to keep it alive every couple of minutes.
-
Worth noting that Netlify’s On-demand Builders provide global persistence across all edge nodes: https://www.netlify.com/blog/2021/10/25/faster-more-reliable-page-loads-with-update-to-on-demand-builders/
Alternatively, CloudFront has 13 Regional Edge Caches that (in my tests with a VPN) seem to be segmented too: https://aws.amazon.com/cloudfront/features/?whats-new-cloudfront.sort-by=item.additionalFields.postDateTime&whats-new-cloudfront.sort-order=desc
CloudFront also offers an Origin Shield product that appears to provide a similar feature but I have not enabled it: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cache-hit-ratio.html#cache-hit-ratio-use-origin-shield
@zachleat right so you can opt into the regional caches if you want to, and there is cost attached to that (iirc there’s also a buried cross region data transit cost). You don’t *have* to do it (choice!)
You can also look at lambda@edge
@cthos I’m very happy with the current setup I think. I’ll enable origin shield once I hit the inflection point where it costs less than my lambda usage (assuming that exists)
@zachleat at some point in this journey, I hope you look into Fastly. Some major improvements over Cloudfront
@tollmanz that is a great callout. I need to dive in there.
@zachleat this is interesting because I keep getting emails about hitting the function invocation limit. From poking around I could see lots of responses that weren’t cached at the edge, but should’ve been.
@benschwarz another distinction here is that I don’t think cached responses are excluded from invocation counts—but I’m not super worried about that since they charge $.60 per million there. It’s the $.18 per GB hour that is biting me (at ~42h per day usage)
correction: cached hits are allegedly not counted in invocation counts: https://vercel.com/docs/pricing/serverless-functions#managing-function-invocations
-
@zachleat is that what we’re supposed to call ourselves? I need to find a place that will let me title myself senior fun developer.
@zachleat Don't tell AWS you're an enterprise developer now, they'll charge you tens of thousands a month! 😳
@rijk luckily no one reads my posts here 😅