does anyone understand cold starts with Lambda fun...
# random
s
does anyone understand cold starts with Lambda funcs? I thought if a function wasn’t invoked for about 4-5 mins or so, it spins down and then the next time it’s invoked, it’s a cold start (indicated by “Init Duration” in CloudWatch Logs). but I have an invocation at 2021-12-17T191339.467-06:00, and that was warm, then another invocation at 2021-12-17T191340.302-06:00 which shows “Init Duration: 525ms”. why would there be a cold start just a second later?
g
The function will only ever handle one request at a time so if the lambda was still handling the other request it will spin up another.
m
A function can stay warm over an hour. The actual length isn't documented but yeah could be concurrency.
Also if you make any change, then of course you get a fresh container. It's also just possible the Lambda service decides time for a new one, since that decision process isn't exposed to us.
IMO the best solution to cold starts is to minimize startup time.
s
I think a larger code bundle size also makes startup slower too, maybe
m
Yup.
s
and this one is.. (checks notes) 35MB. which is kinda ridiculous
m
What language? Could just be some dependency or if you have installed the entire aws-sdk.
s
JavaScript. probably just a poor job at tree-shaking
m
aws-sdk is big if you install the entire thing. Three things you can do there: 1. if using a bundler like esbuild, mark it as an external and depend on the "native" aws-sdk 2. if using v2, install the client (i.e. 'aws-sdk/clients/dynamodb' instead of 'aws-sdk') 3. Use v3 (although it's still kinda big)
s
it’s not the SDK I don’t think. oh wow.. it’s not at all what I expected. it’s
@sentry/cli
. that shouldn’t even be in there
m
When I was teaching devs nodejs, I was constantly having to remove npm as a dependency from their projects because for some reason they liked to type
npm i npm
and didn't realize what had happened.
s
haha
man, I’m glad we had this conversation, because it prompted me to look again at the bundle
I always assumed it was the UI library, as those are rather hefty
m
Glad to help!
s
rxjs is the next biggest offender at 16.6MB
m
So you're doing some SSR in Lambda? That's pretty cool, not something I've done.
s
yeah, it’s been a bit of a challenge, but it’s great when it’s set up properly
the old version of our app costs us like $400/mo to run three 24/7 containers. ugh. so I’m going 100% serverless and never going back 😄
m
That's awesome. Best of luck with it! We started doing serverless backend processing and some APIs and the cost of all that (including engineering time spent and TCO) is tiny compared to our old EC2 stuff.
s
thanks 🙂 yeah, it’s amazing how nimble everything is. and cheaper.. at least until you scale. which is why you’d better price your service properly
g
Yeah running containers 24/7 was a death sentence especially when you have a very spiky app for a few minute intervals based on when you are running an event or something of the sorts 😅
s
definitely
r
Re setting aws-sdk as external, be careful because your external dependency will be upgraded in the lambda runtime automatically which has been known to break things on occasion. Personally I'm much preferring the modular, treeshakeable v3 sdk
s
@Ross Coundon yes, v3 sdk is great, despite its flaws
m
I've done all three and tbh haven't noticed a big difference. Been trying to stick with sdk v3 but there are definitely some pain points.