-
Notifications
You must be signed in to change notification settings - Fork 8
How to run hnswlib node on AWS Lambda
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.
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-image.html
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]}"}
Create a repository in Amazon Elastic Container Registry (ECR) and upload the image.
![](https://user-images.githubusercontent.com/5562409/233763747-c1aa827a-6aa5-43f1-9db7-2dcf9d5aa8ed.jpg)
![](https://user-images.githubusercontent.com/5562409/233763904-33603084-78c4-43ef-9489-9ef130bafcc9.jpg)
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 ...
Create a lambda function with the image pushed to ECR.
![](https://user-images.githubusercontent.com/5562409/233764130-71c31ab5-a685-4e49-adf4-a0f75b770956.jpg)
![](https://user-images.githubusercontent.com/5562409/233764133-b3f80c0e-e463-4325-a2d2-f467838b53f0.jpg)
If you can check the operation on the Test
tab of the created lambda function, the work is complete.
![](https://user-images.githubusercontent.com/5562409/233764163-8ba19c1b-b1c2-4d5b-9c6a-1894cc535913.jpg)