How to use layers with Lambda functions?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to talk about layers for lambda functions lambda layers are very useful and they give us two advantages first we can extract some code from our lambda function into a layer and then this layer can be used across several functions so if you have some module or a library and we want to reuse it across several functions and bundle it with them we can create a layer and then use it in these functions the other benefit is that our bundle of our lambda becomes much smaller since we extract the code and put it in a layer it means the lambda function will have less code that we'll need to deploy every time we have data function to learn how layers work we'll look at this function called demo function first let's get started by looking how it works this lambda function accepts one parameter URL and that would be a URLs and image and with the salam the function does is first downloads this image into variable which will be a buffer then it calls size off which is a module from NPM that we'll take a look at in a second and what this module does is returns an object that contains the width and the height of this image and finally we call gate direction which is a very simple function that returns one of three strings if the width is identical to the height it will return square if the width is larger than height it will return horizontal and if the height is larger than the width it will return the string vertical so the handler is very simple only three lines but we use some modules and utilities that we include in our lambda function at the moment but we want to extract them into a layer so first we use size off which is a function from image size this is a library installed room and p.m. by running NPM install image size and what it does is simply returns an object that contains a width and height of the image we have it currently here in not modules it's called image size want to take this library and extract it into a layer so it won't be part of the bundle of our lambda function we also use two functions from the utils file and this is a file that is also part of our lambda function in the moment and it returns two functions get image that uses Axios and what it does is downloads the image and then creates a buffer out of it and get direction which I mentioned earlier which takes two arguments and then compares them and returns a string you want to take this utils file and also extract it to our lamda layer so then result should be that will have the sender function with the three lines that we have here and all the dependency we use in our handler will be in our layer and that will give us an opportunity to look at how we can extract not modules into the layer but also how can we extract and then use code from a regular file not from an NPM module like we have here with utils so before we get started let's take a look at how our function works if you'll go to here we can see that there's a test event that accepts a single argument URL so let's pick a random image let's say this one will copy the image address and then pass it to our lambda function now when we run our function we get the result vertical and that is because the height of this image is larger than the width so our function works so let's try to take all of the code that we want to extract and separate it into a layer first let's switch to the layers of the AWS console currently we don't have any let's create a new layer we'll call it image details when we create a layer it gives us an option to upload a zip file which will contain the code of our layer so let's create a zip file so in the console we have an empty folder so first of all we want to create an NPM package now that we created our package we want to install image size which we use to extract the width and height of the image [Music] then we also want to add Axios which we use to download the image and finally we want to create a utils file that is going to contain our two functions that we saw earlier so it just copy-paste dysfunctions into our new file we'll go back to our function and let's copy the contents of the utils file now our lair contains all the code that we want to extract from our lambda function and load it from the lair but there's one more thing that we need to do and the thing is to create a folder named no GS and the name of the folder is not accidental this is the name that lambda requires us to use when we want to load node modules so we'll move node modules to the no GS folder and also the package.json and package log JSON once these files are placed in the non GS folder we can just assume that when we require a library it will be required from the node GS folder knowledge bottles so let's create a zip file which we can upload to our layer we created our layer dot zip file and now let's create our layer and upload this file that we created so we'll go back to the console and choose the file that we want to upload we also need to specify the runtimes that this layer will be compatible with should work with nodejs ten and twelve so we'll add both of them and then create the layer a layer was created and as you can see the layers also have versioning so this is version one of the layer and that is very convenient because it allows us to use different versions of the same layer across different functions let's switch back to our lambda function and attach the layer that we created well scroll to the top you'll see that there is an option to list the layers and to add new ones so let's choose add a layer under compatible layers it should list the layer that we created image utils let's add it now that we added the layer and save the function we can start getting rid of some of the code that we have that is now part of the layer so let's see what we can remove first we copied the node modules folder with all its contents to our lair so we no longer need it we also copied package log to JSON and package JSON and we also created a utils file it is now under layer and we no longer need here so we'll delete all of these files and now we are left only with the handler and the imports of the code that we're going to use one last thing that we'll need to change is how we require the utils file because when we require it this way number will try to load it from the same folder if we want to tell lambda that we want the utils far from our lair we need to use the opt folder this is the folder that will contain the code that we extracted into our lair excluding the Nod modules which we already saw that needs to go to the node.js folder so let's try to save and then test our function again once again we get vertical as a result let's try it with a different image we'll now find an image that should return horizontal as the value let's try it out a copy image address and pass it as the URL attributes and once we run it we get horizontal back so now our bundle is significantly smaller and every time we deploy the function it will work much faster because this much less code to deploy that's the first advantage the second advantage is that now all the code that we have in our layer we can reuse it across other functions if we have functions that need to load Axios or the image size library we can just attach the layer that we created to them and use these modules as if they are part of the lambda function the important thing to remember is that we need to place our node modules within a folder called no GS and second when we load files to be created we have to tell them that they are in the opt folder and then lambda will know that they should be loaded from our layer so this is the video for today we introduced a very powerful concept of a WS lambda which is layers layers allow us to extract code from a lambda function into a separate layer and then we use it across several functions if you enjoyed this video please press the like button and also I'm going to continue uploading videos about different AWS services so if you would like to receive updates when new videos are posted please sign up to this channel
Info
Channel: BiteSize Academy
Views: 11,816
Rating: undefined out of 5
Keywords: aws, lambda, layers, npm, nodejs, node_modules, amazon, web, services, javascript
Id: i12H4cUFudU
Channel Id: undefined
Length: 8min 53sec (533 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.