Scaleway DocumentationComputeFunctionsHow to
Upload a function using the Serverless.com framework

Jump toUpdate content

How to upload your function using Serverless.com framework

This plugin allows Serverless Framework users to deploy their functions and containers to Scaleway Functions with a simple serverless deploy. Serverless Framework will handle everything from the namespace creation to the function/code deployment by calling APIs endpoint under the hood.

Requirements:
  • You have an account and are logged into the Scaleway Console
  • You have configured your API keys for your Project
  • You know your Project ID
  • You have installed node.js
  • You have installed Serverless CLI on your computer (npm install serverless -g)

Create a project folder

Serverless Framework enables you to create a project using a specific template.

  1. Start by creating and changing into ~/my-srvless-projects:
# mkdir ~/my-srvless-projects
# cd ~/my-srvless-projects
  1. Create a simple Python 3 template by running:
serverless create --template-url https://github.com/scaleway/serverless-scaleway-functions/tree/master/examples/python3 --path myService

You can find other Scaleway templates on our Github.

How to edit the serverless.yml file

Note:

provider.name and plugins MUST NOT be changed, they enable us to use the scaleway provider

This file contains the configuration of one namespace containing one or more functions (in this example, only one) of the same runtime (here python3) The different parameters are:

  • service: your namespace name
  • provider.runtime: the runtime of your functions (check the supported runtimes above)
  • provider.env: environment variables attached to your namespace are injected to all your namespace functions
  • scwToken: Scaleway token you got in prerequisites
  • scwProject: Scaleway project id you got in prerequisites
  • package.patterns: usually, you do not need to configure it. It enables to include or exclude directories from the deployment
  • functions: Configure of your fonctions. It is a yml dictionary, with the key being the function name
    • handler (Required): file or function which will be executed. See the next section for runtime specific handlers
    • env (Optional): environment variables specifics for the current function
    • minScale (Optional): how many function instances we keep running (default: 0)
    • maxScale (Optional): maximum number of instances this function can scale to (default: 20)
    • memoryLimit: ram allocated to the function instances. See the introduction for the list of supported values
    • runtime: (Optional) runtime of the function, if you need to deploy multiple functions with different runtimes in your Serverless Project. If absent, provider.runtime will be used to deploy the function.
    • events (Optional): List of events to trigger your functions (e.g, trigger a function based on a schedule with CRONJobs). See events section below

How to configure your Functions Handler

Based on the chosen runtime, the handler variable on function might vary. For more detail on how to package your dependencies please refer to How to package your function and upload it as a Zip.

Node

  1. Path to your handler file (from serverless.yml), omit ./, ../, and add the exported function to use as a handler:
# ls
- src
- handlers
- firstHandler.js => module.exports.myFirstHandler = ...
- secondHandler.js => module.exports.mySecondHandler = ...
- serverless.yml
  1. In the serverless.yml file, launch:
provider:
# ...
runtime: node8 # or node10 or node14
functions:
first:
handler: src/handlers/firstHandler.myFirstHandler
second:
handler: src/handlers/secondHandler.mySecondHandler
  1. To add dependencies to your function, include the node_modules folder in your package list.
package:
patterns:
- '!.gitignore'
- '!.git/**'
- './node_modules/**'

Python

  1. Similar to node, path to handler file src/testing/handler.py:
- src
- handlers
- firstHandler.py => def my_first_handler
- secondHandler.py => def my_second_handler
- serverless.yml
  1. In serverless.yml, launch:
provider:
# ...
runtime: python3 # or python for python 2.7
functions:
first:
handler: src/handlers/firstHandler.my_first_handler
second:
handler: src/handlers/secondHandler.my_second_handler

Golang

If you have the following structure, the path to your handler’s package should resemble to:

- src
- testing
- handler.go -> package main in src/testing subdirectory
- second
- handler.go -> package main in src/second subdirectory
- serverless.yml
- handler.go -> package main at the root of project

Your serverless.yml functions should be similar to:

provider:
# ...
runtime: golang
functions:
main:
handler: "."
testing:
handler: src/testing
second:
handler: src/second

How to configure your events

With events, you may link your functions with specific triggers, which might include CRON Schedule (time-based), MQTT Queues (publish on a topic that will trigger the function), S3 Object update (upload an object will trigger the function).

Note:

We do not include HTTP triggers in our event types, as a HTTP endpoint is created for every function. Triggers are just a new way to trigger your Function, but you will always be able to execute your code via HTTP.

Below is a list of supported triggers on Scaleway Serverless, and the configuration parameters required to deploy them:

  • schedule: Trigger your function based on CRON schedules
    • rate: CRON Schedule (UNIX Format) on which your function will be executed
    • input: key-value mapping to define arguments that will be passed into your function’s event object during execution.

To link a Trigger to your function, you may define a key events in your function:

functions:
handler: myHandler.handle
events:
# "events" is a list of triggers, the first key being the type of trigger.
- schedule:
# CRON Job Schedule (UNIX Format)
rate: '1 * * * *'
# Input variable are passed in your function's event during execution
input:
key: value
key2: value2

You may link Events to your Containers too (see section Managing containers below for more information on how to deploy containers):

custom:
containers:
mycontainer:
directory: my-directory
# Events key
events:
- schedule:
rate: '1 * * * *'
input:
key: value
key2: value2
Tip:

You may refer to the follow examples:

How to access to the Logs

The serverless logs command lets you watch the logs of a specific function or container. Pass the function name you want to fetch the logs for with --function

serverless logs --function <function_name>`

How to delete a project

To remove a project, you may run the following command:

serverless remove

This will remove all your functions, as well as your Scaleway Functions namespace.

Note: Your Scaleway Container Registry namespace will not be removed, you will have to delete it manually either via Scaleway console, or Scaleway container registry's API.