This project aims to provide a Function as a Service (FaaS) solution with minimal infrastructure requirements, avoiding the overhead of Kubernetes.
In the modern era, event-driven, microservice-based architecture is gaining popularity. FaaS allows you to trigger code execution based on events. However, hosting FaaS on a Kubernetes cluster can be costly. NodeJS-FAAS offers a lightweight alternative, providing an efficient lambda function solution without the need for Kubernetes. It is not intended to replace solutions like OpenFaaS but to offer a simpler, more resource-efficient option.
NodeJS-FAAS uses the built-in Node.js VM module to execute code.
You can set up NodeJS-FAAS using Docker or Containerd.
To pull the Docker image:
docker pull shreyasnayak21/nodejs-faas:1.2.0
To run the Docker container:
docker run -d -p 9256:9256 -e JWT_KEY=1X2A0p84VmmLF3NYz3uHPx1hLuhT2U24 -v functions:/opt/app/functions shreyasnayak21/nodejs-faas:1.2.0
To pull the image with Containerd:
nerdctl pull shreyasnayak21/nodejs-faas:1.2.0
To run the container with Containerd:
nerdctl run -d -p 9256:9256 -e JWT_KEY=1X2A0p84VmmLF3NYz3uHPx1hLuhT2U24 -v functions:/opt/app/functions shreyasnayak21/nodejs-faas:1.2.0
You can also run the source code using pm2
:
npm install
npm start
To start the NodeJS-FAAS using pm2
:
pm2 start index.mjs --name NodeJS-FAAS
Currently, NodeJS-FAAS provides an interface over an HTTP API. You can create a lambda function or upload code in an mjs
file to the instance and execute the function using the API. Future plans include adding NATS.io or Pub/Sub for function triggers.
const axios = require('axios');
function main(event, context, callback) {
if (event === "get_nasa_image") {
const { key } = context;
axios.get(`https://api.nasa.gov/planetary/apod?api_key=${key}`)
.then(response => {
if (callback) callback({
explanation: response.data.explanation
});
})
.catch(error => {
if (callback) callback({
error: error.message
});
});
}
}
main(G_EVENT_NAME, G_CONTEXT, G_CALLBACK);
To generate a token, visit the JWT Generator Website. Use the JWT_KEY you provided for the container and input the following content in the body, adjusting the parameters as needed:
{
"expire_at": "1735713720",
"issued_at": "1721994397",
"issuer": "shreyas",
"namespace": "neo"
}