Configuring
Namespace
The first step is to create a Serverless instance, which maps to a function namespace. All your functions will be deployed to this namespace. When deploying, the scw-serverless CLI will look for a Serverless instance in the global scope.
import os
from scw_serverless import Serverless
app = Serverless(
"my-namespace",
env={
"NON_CONFIDENTIAL_PARAMETERS": "My Param"
},
secret={
"SCW_SECRET_KEY": os.environ["SCW_SECRET_KEY"],
"MY_SECRET_KEY": os.environ["MY_SECRET_KEY"]
},
})
- class scw_serverless.app.Serverless(service_name: str, env: dict[str, Any] | None = None, secret: dict[str, Any] | None = None)
Manage your Serverless Functions.
Maps to a function namespace. Parameters will be scoped to the namespace.
- Parameters:
service_name – name of the namespace
env – namespace level environment variables
secret – namespace level secrets
- func(**kwargs: Unpack[FunctionKwargs]) Callable
Define a Serverless handler and its parameters from the keyword arguments.
See
FunctionKwargs
for all possible parameters.Example
app = Serverless("example") app.func(privacy="public", env={"key": "value"}) def handler(event, context) ...
- get(url: str, **kwargs: Unpack[FunctionKwargs]) Callable
Define a routed handler which will respond to GET requests.
- Parameters:
url – relative url to trigger the function
- post(url: str, **kwargs: Unpack[FunctionKwargs]) Callable
Define a routed handler which will respond to POST requests.
- Parameters:
url – relative url to trigger the function
- put(url: str, **kwargs: Unpack[FunctionKwargs]) Callable
Define a routed handler which will respond to PUT requests.
- Parameters:
url – relative url to trigger the function
Functions
To configure your serverless functions, you can provide keyword arguments to the decorators. The Function name that will appear in the Scaleway console will be the name of your function’s handler.
- class scw_serverless.config.function.FunctionKwargs
Typed arguments supported by Scaleway functions.
Note
Some parameters may not be supported by a specific backend.
- Parameters:
env – Environment variables to be made available in your function.
secret – Secret environment variables to be made available in your function.
min_scale – Minimum replicas for your function.
max_scale – Maximum replicas for your function.
memory_limit – Memory (in MB) allocated to your function.
timeout – Max duration to respond to a request.
description – Description. Defaults to the function docstring if defined.
http_option – Either “enabled” or “redirected”. If “redirected” (default), allow http traffic to your function. Blocked otherwise.
See also
app = Serverless("example")
app.func(
privacy="public",
env={"key": "value"},
secret={"key": "value"},
min_scale= 0,
max_scale= 5,
memory_limit= 256,
timeout= 300,
description= "Lores Ipsum",
http_option= "enabled", # https only
)
def handler(event, context):
# Do Things
return {"body": "Hello World"}
Triggers
By default, Scaleway Functions are given an HTTP endpoint that can be called to execute your function. In addition, you can set up additional triggers that will run your function on specific occasions.
Cron triggers
To create a function that will run periodically, you can use the schedule decorator. If do not want your function to be publicly available, set the privacy to private.
app.schedule("0 8 * * *", privacy="private")
def handler(event, context):
...
- class scw_serverless.config.triggers.CronTrigger(schedule: str, args: dict[str, Any] | None = None, name: str | None = None)
Cron trigger which will execute a function periodically.
- Parameters:
schedule – The Cron expression.
args – Data to be sent in the body.
name – Name to give to your resource.
See also
Scaleway Documentation <https://developers.scaleway.com/en/products/functions/api/#create-a-cron-trigger-for-your-function>