The function above only processes GET requests, as declared in its code. Other requests will return the defined error message:
curl -X POST http://localhost:8080> Invalid method!
Local testing allows you to run your code on your development environment with a short deploy time.
Scaleway Serverless Functions can run outside Scaleway infrastructures. This is useful to set up, develop, and test functions locally. You can run functions directly on your machines to debug them and analyze logs.
Each local testing framework is written in its respective language and emulates the calls made to functions by the Scaleway infrastructure. The result is a language-specific executable or script that users can connect to with their favorite debugging and logging tools.
Refer to the NodeJS local testing repository for more information on testing your function locally using Node.
Quickstart
Install the Scaleway Serverless Functions package using npm
:
npm i @scaleway/serverless-functions
Add the following code to the file containing your handle:
For ES modules
// handler.jsimport { pathToFileURL } from "url";function handle(event, context, callback) {return {statusCode: 201,body: JSON.stringify({message: "Hello World!",}),headers: {"Content-Type": "application/json",},};}// This part will execute when testing locally, but not when the function is deployed onlineif (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:
// handler.jsconst url = require("url");module.exports.handle = (event, context, callback) => {return {statusCode: 201,body: JSON.stringify({message: "Hello World!",}),headers: {"Content-Type": "application/json",},};};// This part will execute when testing locally, but not when the function is deployed onlineif ("file://" + __filename === url.pathToFileURL(process.argv[1]).href) {import("@scaleway/serverless-functions").then(scw_fnc_node => {scw_fnc_node.serveHandler(exports.handle, 8080);});}
In a terminal, run the command below to execute your file and start the local webserver:
node handler.js
In another terminal session, run the command below:
curl -X GET http://localhost:8080
The function returns the content of its body:
{"message":"Hello World!"}%
Refer to the Python local testing repository for more information on testing your functions locally using Python.
Quickstart
Install the Scaleway Serverless Functions package using pip
:
pip install scaleway-functions-python
Add the following code to the file containing your handle:
# handler.py# Standard entrypoint to a Scaleway serverless functiondef handler(event, context):if event["httpMethod"] != "GET":return {"statusCode": 405, "body": "Invalid method!"}return "Hello World!"if __name__ == "__main__":# The import is conditional so that you do not need# to package the library when deploying on Scaleway Functions.from scaleway_functions_python import locallocal.serve_handler(handler, port=8080)
In a terminal, run the command below to execute your file and start the local webserver:
python handler.py
In another terminal session, run the command below:
curl http://localhost:8080
The function returns the expected output:
Hello World!
The function above only processes GET requests, as declared in its code. Other requests will return the defined error message:
curl -X POST http://localhost:8080> Invalid method!
Refer to the Go local testing repository for more information on testing your functions locally using Go.
Quickstart
Install the Scaleway Serverless Functions package using go get
:
go get github.com/scaleway/serverless-functions-go
In a new folder, create a file named handler.go
and add the following code to it:
package handlerimport ("encoding/json""net/http")// This handle function comes from our examples and is not modified.func Handle(w http.ResponseWriter, r *http.Request) {response := map[string]any{"message": "We're all good","healthy": true,"number": 4,"headers": r.Header,}responseBytes, err := json.Marshal(response)if err != nil {w.WriteHeader(http.StatusInternalServerError)return}// Set the header explicitly depending the returned dataw.Header().Set("Content-Type", "application/json")// Customize status codew.WriteHeader(http.StatusOK)// Add content to the response_, _ = w.Write(responseBytes)}
Create a main.go
file in a new cmd
subfolder, then add the following code to it:
package mainimport (// "localfunc" is the module name located in your go.mod. To generate a go.mod with "localfunc" as a name, you can use the following command : go mod init localfunc.// Otherwise, you can replace "localfunc" with the name of your own module.localfunc "github.com/scaleway/serverless-functions-go/examples/handler""github.com/scaleway/serverless-functions-go/local")func main() {// Replace "Handle" with your function handler name if necessarylocal.ServeHandler(localfunc.Handle, local.WithPort(8080))}
Run the commands below to generate a mod
file, then automatically add the modules to it:
go mod init localfunc && go mod tidy
Run the command below to create a new function for local testing:
go run cmd/main.go
In another terminal session, run the command below:
curl http://localhost:8080
The function returns the expected output:
{"headers": {"Accept": ["*/*"],"Forwarded": ["for=localhost:8080;proto=http"],"K-Proxy-Request": ["activator"],"User-Agent": ["curl/8.4.0"],"X-Envoy-External-Address": ["localhost:8080"],"X-Forwarded-For": ["localhost:8080","127.0.0.1","127.0.0.2"],"X-Forwarded-Proto": ["http"],"X-Request-Id": ["5283e1ee-e88e-11ef-ab6e-acde48001122"]},"healthy": true,"message": "We're all good","number": 4}
Testing your function locally will create a local web server that listens to a given port.
You need to be able to install packages with the dependency manager of the runtime you use, such as pip
, go get
, etc.
Each local testing library tries to best replicate the Serverless Functions environment, but cannot fully emulate the surrounding infrastructure. Therefore, although being able to run your function locally is a good indicator that it will work, it may still fail due to the execution environment used in Scaleway Serverless Functions.
The most significant difference is that, when running in Scaleway Serverless Functions, your function is packaged into a custom Docker image using our build pipeline. This means that at runtime, your function may be launched in a different operating system, with different libraries available to your local development environment.
Performance during local testing can differ from the deployed Serverless Functions environment, and will involve limitations around resource usage and quotas.
CPU/memory settings do not apply when testing functions locally.