Create your first function

This guide will help you create and build your own function using Node.js.

Disclaimer: at the moment, this guide will give you a ready-to-use rootfs for Firecracker. We're working on the distribution of this rootfs.

Requirements

Create the function code

Create a directory on your machine where your function code will be stored, and initialize a new npm project:

mkdir -p /tmp/my-first-function
cd /tmp/my-first-function
npm init -y

Now, create a file handler.js with the following content :

exports.handler = async function(req, res) {
    // `req` is an Express request object
    // See: https://expressjs.com/en/api.html#req.app
    console.log(req.body)

    // `res` is an Express response object
    // See: https://expressjs.com/en/api.html#res.app
    console.log(res)
    
    // Here you have full control on the response.
    // You can return anything you want, but you always need to set at least the response code
    return res.status(200).send("My first function !")
}

Your function is ready to be packaged.

Build the function package

To package your function, execute the following command :

morty build --name my-first-function --runtime node-19 .
  • --name my-first-function: the name of the function
  • --runtime node-19 : here you indicate that you want to use the node-19 runtime.
  • . : the path to the function code. As we are in the same folder, we use . here.

You should have a file my-first-function.ext4.lz4 into your current working directory.

Create your first function with Firecracker (without RIK)

This guide will help you set up your first function into a Firecracker microVM, and trigger it.

Requirements

  • firecracker installed into your $PATH
  • docker

Steps

First of all, you should create a temporary directory on your host :

mkdir -p /tmp/getting-started-fc
cd /tmp/getting-started-fc

For the rest of the commands above, we assume that you are in the /tmp/getting-started-fc directory.

Copy the start-vm.sh script to your directory :

cp /path/to/polyxia-org/repo/scripts/start-vm.sh .

Download the kernel :

curl -k -o vmlinux.bin https://162.38.112.10:13808/swift/v1/AUTH_9ab97b0fd6984ca2a6261286b66f4cae/polyxia-dev/vmlinux

Clone the runtimes :

# SSH
git clone git@github.com:polyxia-org/morty-runtimes.git

# HTTPS
git clone https://github.com/polyxia-org/morty-runtimes.git

You should have the following folder architecture :

ls -la
#...
drwxr-xr-x  4 debian debian      4096 Feb 22 21:57 morty-runtimes
-rwxr-xr-x  1 debian debian      3240 Feb 22 21:59 start-vm.sh
-rw-r--r--  1 debian debian  45614488 Feb 22 22:07 vmlinux

Now, you simply need to run the following command to build and run the template function into a microVM. The <TEMPLATE> argument is optional and if you omit it, node-19 will be automatically selected for you. You can check the list of the available templates here.

# You must run the script as root
sudo bash start-vm.sh <TEMPLATE>

# Once everything has booted, you should see at the end of the logs the following lines : 
INFO[0000] Started process /usr/local/bin/node /app/index.js (pid=482) 
INFO[0000] Alpha server listening on 0.0.0.0:8080 

From another terminal, you can now invoke your function by using the following command :

curl http://172.16.0.2:8080

You should see a similar output :

{
  "payload": "My first function !",
  "process_metadata": {
    "execution_time_ms": 19,
    "logs": [
      "I can log some messages..."
    ]
  }
}

To exit your VM, get the PID of your firecracker process and kill it :

ps -auxf | grep firecracker
sudo kill -9 <PID>