AWS Lambda: How to create a nodejs layer for axios
In previous articles, we looked at how AWS Lambda and CloudWatch can be leveraged as a powerful troubleshooting tool for integrations between various SaaS products. Since some of you may not be familiar with creating a layer, I wanted to share the steps that are needed.
More in-depth information about AWS Lambda layers can be found here.
In this article, I will provide the steps to create a nodejs layer for AWS Lambda. We will use axios, a popular package that allows for outgoing web services and API calls, as an example.
The first step is to install nodejs on a host. I am using the EC2 Amazon Linux 2023 AMI.
The steps to install nodejs for Amazon Linux can be found here.
After nodejs is installed, we should be able to run the following command successfully:
Now we need to execute the following commands:
1- Create a new subdirectory called nodejs: mkdir nodejs
2- Change directory to nodejs: cd nodejs
3- Use npm to install axios: npm install axios
4- Exit the subdirectory with: cd ..
5- Create the layer with zip: zip -r axios.zip nodejs/
We can see that the axios.zip layer file has been created. It includes the node_modules subdirectory with the required dependencies for axios.
Now we just need to import the axios layer into Lambda so we can reference it using its ARN from within nodejs Lambda Functions.
I hope that you found this article helpful, especially if you are not familiar with AWS Lambda layers and if you wanted to leverage the AWS Lambda Functions that we covered in previous articles.