AWS Lambda: Using External Libraries
Intro
A common scenario with AWS Lambda is the need to use an external library in your function (AWS SDK functions are available by default) such as Axios. However Lambda will not detect and install NPM packages for you automatically if you require them in your functions. In short you have to create a zip package that contains your function code and it's dependencies and upload that package into Lambda.
Setup and Organization
This section is just a suggestion, not technically required
My suggestion is that you create a new folder on your system to contain all of your Lambda functions for your project, and then inside that folder a sub-folder for each function. You should create a git repository in the root of the Lambda folder.
The advantages of this are the following:
- You can do proper version control of all your projects Lambda functions (skip the clunky Lambda versioning feature)
- You can write your functions in your IDE of choice (writing code inside the Lambda editor is a pain)
Local Folder for Function
You start by creating a local folder for your function, and open that inside of your IDE of choice. Then you create an index.js
file inside of your folder. Your function will go in this index.js file. Let's use the following API request that requires axios.
const axios = require('axios'); // highlight-line
exports.handler = async event => { // highlight-line
let config = {
method: 'get',
url: 'https://swapi.dev/api/people/1/',
headers: {}
};
return axios(config)
.then((response) => {
return (response.data.name);
})
.catch((error) => {
console.log(error);
});
}
As we can see highlighted, this API request requires Axios.
Remember that we need to insert the Lambda exports handler in our function, just like we would in Lambda.
Install Dependencies
Using your console navigate to your function folder and then install your dependencies
npm install axios --save
This will install axios and it's dependencies into a node-modules
folder, and also create a package-lock.json
file.
Repeat this step for any additional dependencies your function requires.
Create ZIP File
Now we need to package our little application into a ZIP file. To do that you can simply use the following console command.
zip -r PACKAGE-NAME.zip .
The period at the end indicates that we are packaging everything in this directory.
This will create a zip package inside our directory. Next head over to Lambda.
Upload ZIP
Inside our Lambda function we have an option to upload a ZIP file
And our little application has been uploaded
and if we test we can see that we have successfully received a response from the API
Comments
Recent Work
Basalt
basalt.softwareFree desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.
BidBear
bidbear.ioBidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.