I wound up having a little talking video about like why I really like using serverless and Lambda functions we've been using Lambda at work for the past like four or five years to deploy basically our entire application right all the apis deployed with lambdas all of our like asynchronous functions that do long running executions running lambdas and overall I think out of the box the Lambda just gives you a lot of stuff that really helps you as the developer not have to focus on the maintainability and monitoring of your software so let's take a step
back and talk about like how do you typically deploy an express application or a nest.js application or any type of like web server usually you have to have an actual like executable so for example with with Express you'd have to like do a node space index.js and that's going to run your Express server and you can rent a VM and you can get that hosted on the VM but there are a lot of downsides to doing that first of all you need to make sure that your Express server never never runs out of memory you
need to make sure that if it were to crash you can restart it right so you need to set up like pm2 or some type of forever script to basically just restarts your express service if it were to crash another issue with having your own like a running express service is let's say you have a hundred concurrent connections like there's 100 people trying to hit your API at the same time and for some reason your API crashes well that's a hundred people who just lost their data right their requests are gone maybe they're trying to
insert data maybe they're trying to retrieve data all that stuff is killed because the server is basically sharing the entire process and when the process gets killed maybe there's a bug in your code you threw an exception that you didn't catch basically all your connections are killed right so and then on top of that you have to worry about scaling how do you scale up your express service if you get a lot of traffic right right now if you have a single VM with a single Express server in there you have to figure out a
way to scale it luckily pm2 has ways to like increase the amount of threads and kind of scales under the hood but at some point you're going to hit a threshold with that vertical scaling approach you have to do horizontal scaling another issue I don't know how many issues Matt now but another issue is you have to be able to monitor your production logs in your memory and CPU usage of the machine right if you're not monitoring those things if you don't have access to production logs and people start doing requests and stuff just starts
throwing errors you have no insight as to what went wrong what were the exceptions thrown you have to basically set that all up yourself I haven't done like VMS in a while but last time I did it I had to set up a lot of stuff to basically take the log files make sure you're rotating them take all the contents of the log files and ship them off somewhere for example I think we ship them off to Elk is like log stash classic search and Cabana and basically you have to do all the stuff manually
just to kind of monitor your systems a lot of downfalls to having your own VM and again you can containerize this and maybe that makes some of this stuff easier but overall it's just a lot of work so what I like to do is we do something called a monolanda where we'll take that express service the entire code that's used for Express and we'll put it in a single Lambda and I will let the Lambda itself do the routing so if I do a request to a certain endpoint on API Gateway that'll invoke my Lambda
and then my lambda's Express router inside will basically figure out okay what do I need to run so what are the benefits of using Lambda and why do I like doing that approach for my apis basically again and a lot of the benefits basically counteract the negatives I said with hosting your own thing for example every execution of your Lambda is going to run in isolation so if you have 100 people hit your Lambda at the same time if one of them throws an exception like a fatal exception it's only that one person who's affected
all the other lambdas that are currently running will not be affected at all so there's a nice isolation that's provided when you run in lambdas which makes your system more resilient to issues in terms of scaling it's pretty easy to like scale up like you can basically create one Lambda and just throw a bunch of traffic to it and you'll see that Lambda start provisioning a bunch of different Runners warming up containers behind the scenes I guess and it'll just kind of handle that traffic for you whether you're just doing like one request a second
or 100 requests a second you'll see the language just scale up automatically for you and you don't have to worry about any of that stuff which is great so another cool thing about lambdas is that you conversion your lambdas which I think allows you to quickly roll back lambdas if something goes wrong so for example if you were to publish a new version 70 and it has a critical bug you can just roll it back to 69 um pretty easily right pretty cool I like that feature lambdas also integrate really well with everything else in
Amazon so if you're at a company or corporation that really likes hosting everything into AWS like it's pretty easy to just start hooking the stuff up and giving it permissions to do what it needs to do inside of Lambda as far as scaling the individual resources of the Lambda I believe you can go to like your configurations and you can actually increase your timeouts of the Lambda you can increase how much storage is in here I think the memory can go up to almost 10 gigs of memory which is crazy I mean the most I
think you probably need is like 756 unless you're doing some crazy computation inside of Lambda and then timeout I believe can go up to 15 minutes so a lot of these services that you might use they send they tend to give you like a hard-coded limit of like 10 seconds or 30 seconds but if you're using Lambda directly you can bump that up to 15 minutes assuming you're not limited by API Gateway and stuff like that so another thing I like about lambdas is that you already you automatically have built-in monitoring so like as request
invocations are coming in you can see how long did that request take and you can also see how much you're going to build for that request okay I believe there's more metrics you can get but you kind of have to become like a guru at the cloud watch um logs in the Lambda insights but there are ways to get more information about your Lambda functions um like how long the execution took how much memory those things used Etc so like I mentioned with the express downfalls it's like you have built-in logging all like already so
if I click on view Cloud watch logs this will take you to a log stream which has every single log of every single invocation of your Lambda so automatically like this stuff prints out all your console logs will print out to the log stream and you can forward these to like elasticsearch or Cabana to have a better way to kind of parse through your logs if you want to there's also um log insights which is a more robust way to search through your logs if you're kind of willing to take the time to figure out
this a query syntax but it's very very useful okay there you go I refresh the page and now if you go to the monitor I can see like all my recent invocations I can see how long this stuff take you can actually go to metrics over here you can see how many implications that I get on a certain time period how long does invocations take were there any errors are there any throttles a lot of good information right so you have all this stuff built in out of the box which I think is just so
much better than using like your own roll your own express service now I will say there are downsides they're using Lambda the first one everyone mentions is the cold starts those can be an issue I will say if your application is actually getting any type of real traffic at some point you'll have enough lambdas that are warm that you will not be like your users will not notice these cold starts right maybe one user out of 100 or 200 requests might notice them because I think these containers stay up for about 20 30 minutes so
if you're actually getting real traffic to your API I don't think a cold starts as big of an issue as a lot of people make it out to be I could be wrong you can call me on the comments but it is something to keep in mind because it can take two to three seconds even more depending on how much stuff you're loading when your node application starts executing and that three to four seconds could be a make or break it for your users trying to buy your product Etc so if you're trying to do
something where load time is like super Mission critical maybe you need to do your own like containers that are just running an Express service or some type of other approach that really makes this stuff as low latency as possible another downside is that your lambdas have like a 250 megabyte limit so we've actually hit into this many times at work where we have a Lambda that needs to generate PDFs that PDF processing needs something called Puppeteer that Puppeteer Library uses chromium which is basically like the web browser and it uses it in a headless mode
and having all that stuff set up in a Lambda layer eats up like a huge portion of that 250 megabyte limit so you can be very limited um sometimes when you just want to do something simple like I need to generate PDF you start hitting all these limits and you just can't do it in Lambda so then you have to go back to the drawing board and be like okay well let's figure out a different way because I can't use lambdas maybe I do need to have like a container that processes various events from like
an sqs queue and generates and turns on these PDFs and saves in the S3 or something but I would say that if you can try to get it working in Lambda first I think it'll be the biggest bang for your buck because like I mentioned all those other pros at the start of this video um yeah I'm sure there's some other stuff I I left out again like you can hook an API Gateway up to this and you can have a fully working rest API just like this you can have it be a custom domain
if you want but this is all using API Gateway to basically host my my express service okay and again I'm only getting charged per invocation and it's like dirt cheap for every time you do a request your API it's pretty Dirt Cheap so that's another benefit I don't have like a computer that's just sitting there and I'm being charged monthly for that those resources that I'm not using I just get charged for every invocation but that can be a double-edged sword because if you end up doing a bunch of different requests like a Cron job
then in that case it might be better to just like have a self-running service you kind of have to crunch the numbers to figure out like what is the better approach in terms of long-term costs but usually landvis are always going to save you a lot of money because you don't have dedicated resources just sitting there doing absolutely nothing um yeah that's all I really wanted to talk about I know I kind of just jumped around but I hope you guys enjoyed that little overview of why I like using lambdas if you enjoyed watching be
sure to give me a comment and like And subscribe and press the Bell icon I have a Discord channel that you can find in the description if you want to talk to me directly or just find a place to ask questions to some other developers and I have a newsletter that you're welcome to subscribe to if you want to get some tips tricks in the future about programming or just JavaScript in general have a good day happy coding