How to bundle a nodejs lambda function? (using esbuild)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll take a look at how to take the node.js code of your lambda function bundle it and deploy it to aws we'll use a tool called lambda build which makes it very easy and fast to bundle archive and upload your code all you need to do is point it to the code you wrote and it will do all the work for you and even publish your code to aws it uses es build which is very fast and efficient at bundling no js code let's start a new project and see how it works add this project that only contains one file which is index.ts and in this file we have this node.js handler for our lumber function so let's try to use lambda build to generate an archive which we can then upload to aws so first let's install lambda build globally and now we can simply run it in our folder the way it works is that it looks for the index.js or ts file in the folder in which it's being run so in our case we have this index.ts file and what lambda build does is first bundles it into a single js file and then creates the archive.zip file so let's create the lam.demo function in our aws console and then upload this archive.zip file to it in the aws console let's create a new function we'll call it lum.demo and by going to upload from and choosing a zip file we can upload the file that was generated for us so now the index.js file contains the bundled code that lambda bill generated let's try to run it as a response we get an empty object which is what we return in our handler so far it's convenient that we have typescript code and lambda build compiles it to javascript for us and creates a bundle but the real benefit we get from bundling is when we start using npm modules so let's install a few i installed these three dependencies uiid faker and low dash and now let's use them in our code i'm also going to install their type libraries but that's not something you need to do if you are not using typescript now our handler uses these three libraries to generate some value for us and return this object this is where the real value of bundling comes in now if you want to upload our function we need to upload not only our code but also the nod modules folder and this folder now contains a lot of code that we don't necessarily need maybe these libraries have the source code or they have some documentation files or just files that are not being used and when we use bundling it eliminates all these files and only includes the code that we actually need so not only it converts our typescript to javascript it also only bundles the code that we need and that makes the cold start time of our lumbar function faster and in general makes the function smaller now let's also create a source folder and move our index.es file into it now that we moved our index.es file we need to let lambda build know where to find it by using the e flag we can pass the new path to lambda build our new archive file was generated and as you can see now it's much bigger at 2.2 megabytes when dealing with bundles it's useful to know where all this size is coming from and we can also use lambda build for that if we'll add the m flag lump the build will generate a meta.json file for us we can use this file to analyze our bundle and see what is responsible for the new size first let's try to upload and run our new lambda function and then use the meta.json file to see what is our bundle consist of so again from the console i'm going to upload the new archive and let's run our function so now every time we run a function we get an object back with these random values that was very easy to install npm libraries and then load them in our code and on the build generated this javascript code for us from our typescript code and as you can see it also added the slow dash code inline to our bundle so everything we need for lambda function is all in the single index.js file now let's see what is the reason our bundle got to two megabytes github in the documentation of lambda build there's a link to analyze your bundle by uploading the method.json file so we'll go to this website and choose our meta.json file now we can see the two megabytes which is the size of our bundle and in here we can see that most of the size comes from node modules of course but specifically from the faker library so we know that if we'll remove faker from our bundle it will be much smaller and this is where lambda layers can become very useful a link to a video i did before about lambda layers and how to use them let's say for our example that we'll move the faker library into a layer so we won't need to include it in our bundle and that's another thing that lambda build helps us accomplish we can use the x flag to exclude any of the npm libraries from our bundle so for example let's exclude faker and now without faker we can see that our bundle became very small again it's 80 kilobytes so for the rest of our example let's just remove faker from our code besides bundling the code for us and creating an archive we can also use lambda build to upload our new bundle to aws for us so we won't need to do it manually by going to the console and uploading the zip file to upload our code directly to lambda we first need to set up the aws cli which you probably already have set up but if you don't have a video that explains how to do it step by step after we configure our credentials we can ask them the build to upload the bundle for us to aws by calling the upload command and specifying the name of the lambda function num the demo in our case we can tell lum the build to bundle our code and then create the archive and automatically upload it to aws we'll use the same flag to let lambda build know about where to find our source code now using the upload command we can create the bundle create the archive and then upload it to aws now let's try to change our code and deploy again so we added a message to our handler now let's try to run lambda build and see if our code was updated so in the console we can go directly to test and try to run our function and now we can see that it's updated to the latest version so now we can simply change our code run lamda build and our function is automatically updated with the latest version it can also be useful to go to packet json and add an additional script let's say we'll call it deploy and just copy the lambda build command into our deploy script now every time we make a change to our code we can simply run the deploy script and it will do everything for us and also upload our code to aws so we can simply run it and then see the latest version of our code already deployed let's look at another example i have this lambda function which returns a json as a response and i have this url that uses api gateway to point to the slumda function this url uses the live alias to point to the version of the lambda function which we want to use in production in the previous video i showed how to set it up and a link to that video now let's say we make a change to our code and let's run the deployment script that we built we can manage the releases of our api using lamda body when we deploy a new change it goes to the version named latest so if we'll point our live values to this version we can then refresh the browser and see our changes deployed however when working on production apps we don't necessarily want our changes to immediately be deployed so instead of using the latest version directly we can publish a new numeric version now this version number three will have the latest code offer function and we can point our live values to it this adds an additional release step to our function and gives us the ability to roll back to any other point in time if we make an additional change to our api and we deploy our changes they won't immediately be released we can now control the release so we'll create a new version and then we'll point the live alias to this new version and if we'll go back to the browser we will now see the latest version of our code and since we created a snapshot of our code at version number three we can simply roll back to this version and it will immediately take effect and if we'll refresh the browser we will now see again version number three of our code if you learned something from this video i would really appreciate it if you leave it a like and don't forget to subscribe to this channel to get updates about new videos that i release
Info
Channel: BiteSize Academy
Views: 36
Rating: undefined out of 5
Keywords: aws, esbuild, lambda, nodejs
Id: FmnFqjBk0to
Channel Id: undefined
Length: 8min 34sec (514 seconds)
Published: Wed Dec 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.