|
1 | 1 | # NodeJS-FAAS |
2 | | -This project aims to achieve something similar to Function as a Service (FaaS) without the overhead of Kubernetes, with extremely low infrastructure requirements. |
3 | 2 |
|
4 | | -# Why NodeJS-FAAS? Why not any other FaaS? |
5 | | -In today's era, event-driven microservice-based architecture is becoming increasingly popular. Using FaaS, you can trigger any code based on events. However, when you host FaaS in a Kubernetes cluster, you also have to consider the cost per function. NodeJS-FaaS is not intended to replace options such as OpenFaaS. Instead, NodeJS-FaaS offers an extremely lightweight lambda function solution. |
| 3 | +This project aims to provide a Function as a Service (FaaS) solution with minimal infrastructure requirements, avoiding the overhead of Kubernetes. |
6 | 4 |
|
7 | | -# How does it work? |
8 | | -NodeJS-FAAS utilizes the built-in Node.js VM module to execute the code. |
| 5 | +## Why Choose NodeJS-FAAS? |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## How It Works |
| 10 | + |
| 11 | +NodeJS-FAAS uses the built-in Node.js VM module to execute code. |
| 12 | + |
| 13 | +## Setup Instructions |
9 | 14 |
|
10 | | -# How to set up |
11 | 15 | You can set up NodeJS-FAAS using Docker or Containerd. |
12 | 16 |
|
13 | | -Pull using Docker: |
| 17 | +### Using Docker |
14 | 18 |
|
15 | | -``` |
16 | | -docker pull shreyasnayak21/nodejs-faas:1.1.0 |
| 19 | +To pull the Docker image: |
| 20 | + |
| 21 | +```sh |
| 22 | +docker pull shreyasnayak21/nodejs-faas:1.2.0 |
17 | 23 | ``` |
18 | 24 |
|
19 | | -Run using Docker: |
| 25 | +To run the Docker container: |
20 | 26 |
|
21 | | -``` |
22 | | -docker run -p 9256:9256 shreyasnayak21/nodejs-faas:1.1.0 |
| 27 | +```sh |
| 28 | +docker run -d -p 9256:9256 shreyasnayak21/nodejs-faas:1.2.0 --env JWT_KEY=QX2A0p84VmmLF3NYz3uHPx1hLuhT2U2K |
23 | 29 | ``` |
24 | 30 |
|
25 | | -Pull using Containerd: |
| 31 | +### Using Containerd |
26 | 32 |
|
27 | | -``` |
28 | | -nerdctl pull shreyasnayak21/nodejs-faas:1.1.0 |
| 33 | +To pull the image with Containerd: |
| 34 | + |
| 35 | +```sh |
| 36 | +nerdctl pull shreyasnayak21/nodejs-faas:1.2.0 |
29 | 37 | ``` |
30 | 38 |
|
31 | | -Run using Containerd: |
| 39 | +To run the container with Containerd: |
32 | 40 |
|
33 | | -``` |
34 | | -nerdctl run -p 9256:9256 shreyasnayak21/nodejs-faas:1.1.0 |
| 41 | +```sh |
| 42 | +nerdctl run -d -p 9256:9256 shreyasnayak21/nodejs-faas:1.2.0 --env JWT_KEY=QX2A0p84VmmLF3NYz3uHPx1hLuhT2U2K |
35 | 43 | ``` |
36 | 44 |
|
37 | | -Alternatively, you can use `pm2` to run the source code: |
| 45 | +### Using `pm2` |
38 | 46 |
|
39 | | -``` |
| 47 | +You can also run the source code using `pm2`: |
| 48 | + |
| 49 | +```sh |
40 | 50 | npm install |
41 | 51 | npm start |
42 | 52 | ``` |
43 | 53 |
|
44 | | -You can run the Node using the following command: |
| 54 | +To start the NodeJS-FAAS using `pm2`: |
45 | 55 |
|
46 | | -``` |
| 56 | +```sh |
47 | 57 | pm2 start index.mjs --name NodeJS-FAAS |
48 | 58 | ``` |
49 | 59 |
|
50 | | -# How to use? |
51 | | -Currently, we provide an interface over [HTTP API](openapi.json). You can create a lambda function or upload code formatted in an `mjs` file to the instance, and you can execute the function using the API. Additionally, there are plans to add NATS.io or Pub/Sub to trigger a function. |
| 60 | +## Usage |
| 61 | + |
| 62 | +Currently, NodeJS-FAAS provides an interface over an [HTTP API](openapi.json). 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. |
52 | 63 |
|
53 | | -# Example Code |
| 64 | +## Example Code |
54 | 65 |
|
55 | | -### Call Http API |
| 66 | +### Calling the HTTP API |
56 | 67 |
|
57 | 68 | ```mjs |
58 | 69 | const axios = require('axios'); |
59 | 70 |
|
60 | 71 | function main(event, context, callback) { |
61 | | - if(event=="get_nasa_image") { |
| 72 | + if (event === "get_nasa_image") { |
62 | 73 | const { key } = context; |
63 | 74 | axios.get(`https://api.nasa.gov/planetary/apod?api_key=${key}`) |
64 | | - .then(response => { |
65 | | - if(callback) callback({ |
66 | | - explanation: response.data.explanation |
| 75 | + .then(response => { |
| 76 | + if (callback) callback({ |
| 77 | + explanation: response.data.explanation |
| 78 | + }); |
| 79 | + }) |
| 80 | + .catch(error => { |
| 81 | + if (callback) callback({ |
| 82 | + error: error.message |
| 83 | + }); |
67 | 84 | }); |
68 | | - }) |
69 | | - .catch(error => { |
70 | | - if(callback) callback({ |
71 | | - error: error.message |
72 | | - }); |
73 | | - }); |
74 | 85 | } |
75 | 86 | } |
76 | 87 |
|
77 | | -main(G_EVENT_NAME,G_CONTEXT,G_CALLBACK); |
| 88 | +main(G_EVENT_NAME, G_CONTEXT, G_CALLBACK); |
78 | 89 | ``` |
79 | 90 |
|
80 | | -# Generate token |
81 | | -Genarte the .env file |
82 | | -`JWT_KEY=MY_SECRET_KEY` |
| 91 | +## Generating a Token |
83 | 92 |
|
84 | | -`apt-get update` |
| 93 | +To generate a token, visit the [JWT Generator Website](https://jwt.io/). Use the JWT_KEY you provided for the container and input the following content in the body, adjusting the parameters as needed: |
85 | 94 |
|
86 | | -`apt-get install jq jw` |
| 95 | +```json |
| 96 | +{ |
| 97 | + "expire_at": "1735713720", |
| 98 | + "issued_at": "1721994397", |
| 99 | + "issuer": "shreyas", |
| 100 | + "namespace": "neo" |
| 101 | +} |
| 102 | +``` |
0 commit comments