AWS series (Lambda- Part 2)
Hello folks,
today we will look at how to use and manage the custom code libraries with our Lambda application. Please do note that this will be my follow-up article to my previous post so if you have not already checked the previous article please head over to the below link
Let's continue,
Some Intro,
As you would already know, AWS lambda refers to a serverless model where you don’t need to worry about the underlying hardware and your function will execute in the specified environment. This means though we are not managing the hardware dependencies we will need to manage code dependencies for our application. In the first place, this might appear complicated but it is pretty straightforward like shipping your dependencies with your code.
Assuming you have the lambda application ready from the previous article with the below file structure, let's see how we can use a third-party library.
(Note: As we are working with a node application, here we will assume the external dependency that we want to use would be a node package. In your case, it could be something else. AWS has a great reference for the same Hint: Look for uploading dependencies with the X app. Here X is your
language eg nodejs/python)
Lets try that:
If you review the file structure you will notice the base root folder does include a package.json file So, we can assume this file structure as any node-based application and we can install the required package by running the npm install command under the root directory. let's try that now with a sample date node package (date-and-time)
> npm install date-and-time
Update the Lambda code to refer to the library. (note we are referring to the library as any normal import in nodejs app)
//import the date library as we would do
let datelib = require('date-and-time');
exports.helloFromLambdaHandler = async () => {
const message = 'Hello from Lambda!';
console.info(`${message}`);
console.info( datelib.format(new Date(), "'ddd, MMM DD YYYY'"));
return message;
}
finally, let's try to run the application.
> sam local invoke helloFromLambdaFunction
and surely it works. below is the output for the same.
As we have run the lambda successfully with a custom library, let's try to build our application for deployment with the below command.
> sam build
The above command will generate a build directory for deployment under the root directory (./aws-sam/build) as shown below.
If you observe the directory structure you will notice the entire node directory got copied under the build directory and Yes we are done!
When we will be deploying your application, we would also be shipping our entire node modules directory with the build.
Are there any caveats?
For small applications with minimum dependencies, this approach works but for larger projects, this would impact the overall upload limits (currently you can upload a max size of 250MB) and will be too much time-consuming. Plus if you have any updates in the code you will still be re-deploying the entire node_module folders without any reason.
The link “Optimizing Node.js dependencies in AWS Lambda | AWS Compute Blog (amazon.com)” is an AWS recommended practices list for optimizing the overall process.
In the next article, we will review how can you avoid shipping the dependencies with the code and ship it separately. This will also allow us to have a common dependency set if we have multiple lambda functions.
Stay tuned! Happy coding...