Check out bidbear.io Amazon Advertising for Humans. Now publicly available 🚀

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.

index.js
const axios = require('axios');

exports.handler = async event => {
  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

upload zip to lambda

And our little application has been uploaded

uploaded zip results

and if we test we can see that we have successfully received a response from the API

luke skywalker response

Amazon Ad Analytics For Humans

Advertising reports automatically saved and displayed beautifully for powerful insights.

bidbear.io
portfolios page sunburst chart