Home

Awesome

Serverless Functions Node

Tests Examples NPM Version Node

Scaleway Serverless Functions Node is a framework which simplifies working with Scaleway Serverless Functions.

It lets you debug your function locally, emulating the input and output format of a deployed Serverless Function.

Note that this library does not deploy functions for you. For that you can see the available deployment methods.

Some useful links when working with Scaleway Functions:

Testing frameworks for Scaleway Serverless Functions in other languages can be found here:

⚙️ Quickstart

Install this package:

npm i @scaleway/serverless-functions

Add the following in the file where your handle is defined:

For ES Modules

import { pathToFileURL } from "url";

function handle(event, context, callback) {
  return {
    statusCode: 201,
    body: JSON.stringify({
      message: "Hello World!",
    }),
    headers: {
      "Content-Type": "application/json",
    },
  };
}

// This will execute when testing locally, but not when the function is launched
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
  import("@scaleway/serverless-functions").then(scw_fnc_node => {
    scw_fnc_node.serveHandler(handle, 8080);
  });
}

For Common JS

const url = require("url");

module.exports.handle = (event, context, callback) => {
  return {
    statusCode: 201,
    body: JSON.stringify({
      message: "Hello World!",
    }),
    headers: {
      "Content-Type": "application/json",
    },
  };
};

// This will execute when testing locally, but not when the function is launched
if ("file://" + __filename === url.pathToFileURL(process.argv[1]).href) {
  import("@scaleway/serverless-functions").then(scw_fnc_node => {
    scw_fnc_node.serveHandler(exports.handle, 8080);
  });
}

Usage

This file will expose your handler on a local web server, letting you invoke your function code directly.

You can do this as follows:

$ node index.js
$ curl -X GET http://localhost:8080
> Hello World!

By running a function locally like this, we can be sure it will work when deployed to Serverless Functions.

📚 Examples

You can find a number of examples in the examples folder. These include:

🏡 Local testing

What this package does:

What this package does not do:

❓ FAQ

Why do I need an additional package to call my function?

The Serverless Functions execution environment wraps your code in an HTTP server, and runs the resulting executable in a Kubernetes cluster behind a load balancer. This introduces a number of layers of abstraction, and additional metadata around the request body. This library attempts to emulate this environment as closely as possible, allowing you to debug your code locally, rather than discovering bugs after deployment.

How my function will be deployed

To deploy your function please refer to the different deployment methods.

Does this package change the runtime behaviour of my function?

No. This package is just for local testing, and will not impact runtime behaviour or performance.

🛟 Help & support

🎓 Contributing

We love to share things with the community. Feel free to raise issues and pull requests on this repo according to the contributing guidelines.

📭 Reach Us

If you want to get in touch with use, you can: