Skip to content

How to run hnswlib node on AWS Lambda

Atsushi Tatsuma edited this page Apr 22, 2023 · 1 revision

Abstract

hnswlib-node is a native addon module using node-addon-api. One way to run hnswlib-node on AWS Lambda is to deploy a container image.

References

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-image.html

Steps

Creating an Image

Create a new Node.js project for hnswlib-node using Yarn.

$ mkdir hnswlib-node-lambda-example
$ cd hnswlib-node-lambda-example
$ yarn init
...
$ yarn add hnswlib-node

Create a new file called index.js. The following is an example code that returns the JSON of the top 3 search results.

index.js:

const { HierarchicalNSW } = require('hnswlib-node');

exports.handler = async (event) => {
  const index = new HierarchicalNSW('l2', 3);
  index.initIndex(5);

  index.addPoint([1, 2, 3], 1);
  index.addPoint([2, 2, 3], 2);
  index.addPoint([3, 2, 3], 3);
  index.addPoint([4, 2, 3], 4);
  index.addPoint([5, 2, 3], 3);

  const query = [0, 2, 3];
  const result = index.searchKnn(query, 3);

  const response = {
      statusCode: 200,
      body: JSON.stringify(result)
  };

  return response;
};

Create a new Dockerfile. You should choose a suitable base image from the ECR public gallery and set the FROM property. Moreover, you need to install python3, make, and gcc-c++ to build native modules.

Dockerfile:

FROM public.ecr.aws/lambda/nodejs:18.2023.04.17.20

COPY index.js package.json yarn.lock /var/task/

RUN yum install -y python3 make gcc-c++ && \
    yum clean all && \
    rm -rf /var/cache/yum
RUN npm install -g yarn
RUN yarn install --production

CMD ["index.handler"]

Build and test the Docker image with the docker commands.

$ docker build -t hnswlib-node-lambda-example:test .
$ docker run -p 9000:8080 hnswlib-node-lambda-example:test
$ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
{"statusCode":200,"body":"{\"distances\":[1,4,16],\"neighbors\":[1,2,4]}"}

Deploying the image

Create a repository in Amazon Elastic Container Registry (ECR) and upload the image.

Execute commands displayed by View push commands:

$ aws ecr ...
$ docker build -t hnswlib-node-lambda-example .
$ docker tag hnswlib-node-lambda-example:latest ...
$ docker push ...

Creating a Lambda function

Create a lambda function with the image pushed to ECR.

If you can check the operation on the Test tab of the created lambda function, the work is complete.