How to create and deploy a function with the Scaleway CLI
Scaleway Command Line Interface Overview
The Scaleway Command Line Interface (CLI) allows you to pilot your Scaleway infrastructure directly from your terminal, providing a faster way to administer and monitor your resources. Scaleway CLI is easy to set up and use, and an essential tool for operating efficiently in your cloud environment. The CLI provides many functionalities, including the ability to create and deploy Serverless Functions.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have configured your SSH key
- You have generated your API key
- You have downloaded and installed the Scaleway CLI
Configuring the CLI
-
Open a terminal and type the following command to configure the Scaleway CLI on your machine:
scw initIf you have already configured the CLI, you will see a message like the following:
Current config is located at /path/to/config.yamlaccess_key: your-access-keysecret_key: your-secret-keydefault_organization_id: ID-of-your-organizationdefault_project_id: ID-of-your-projectdefault_region: your-default-region (fr-par)default_zone: your-default-zone (fr-par-1)Do you want to override the current config? (Y/n): -
Type
n
and go directly to the Creating a functions namespace step of this procedure if you do not want to edit your configuration.If you have not yet configured the CLI, you will be guided through the configuration process via a series of questions:
Enter a valid secret-key or an email -
Enter either the email address for your account (you will then be asked for your password), or your API secret key:
To improve this tool we rely on diagnostic and usage data.Sending such data is optional and can be disabled at any time by running "scw config set send-telemetry=false".Do you want to send usage statistics and diagnostics? (Y/n):Type
y
orn
.To fully enjoy Scaleway CLI we recommend you install autocomplete support in your shell.Do you want to install autocomplete? (Y/n):Type
y
orn
.To enable autocomplete, scw needs to update your shell configuration.What type of shell are you using (default: zsh): -
Press enter if the default shell type displayed is the one you are using. Otherwise, enter the correct shell type you are using. This should complete the configuration process:
Initialization completed with success.
Creating a function
- Run the following command in your terminal to create a functions namespace:
scw function namespace create name=<YOUR_NAMESPACE_NAME>
An output similar to the following displays:
ID 6e74dbb7-9323-46bb-abc7-5c0105f4e70eName <YOUR_NAMESPACE_NAME>OrganizationID 55e28409-776f-4f65-a41e-6c5fc58b4076ProjectID 55e28409-776f-4f65-a41e-6c5fc58b4076Status pendingRegistryNamespaceID -RegistryEndpoint -Description -Region fr-par
- Run the following command in your terminal to create a function:
scw function function create name=<FUNCTION_NAME> namespace-id=<NAMESPACE_ID> runtime=<YOUR_RUNTIME>
An output similar to the following displays:
ID 717fb5f9-95c3-4c0b-84db-2321283e46dcName <FUNCTION_NAME>NamespaceID cee31af8-d43e-4078-8f55-c921a9311f47Status createdMinScale 0MaxScale 5Runtime python310MemoryLimit 256CPULimit 140Timeout 5 minutesHandler handler.handlePrivacy publicDescription -DomainName namespace0leshy-cli-fn-functionname.functions.fnc.fr-par.scw.cloudRegion fr-parHTTPOption enabledRuntimeMessage -
Refer to this page to see all the available runtimes
- Create a zip file containing your function’s code by following this procedure.
For testing purposes, you can create a zip archive with a simple Python file named handler.py
inside, that contains the following code:
def handle(event, context):return { "body": { "message": 'Hello, world', }, "statusCode": 200,}
Deploying a function
- Run the following command to deploy your function with the zip archive you just created:
scw function deploy name=<FUNCTION_NAME> namespace-id=<NAMESPACE_ID> runtime=<YOUR_RUNTIME> zip-file=path/to/archive/<ZIP_FILE>
The deployment process can take up to three minutes.
- Run the following command to call your function:
curl <YOUR_FUNCTION_ENDPOINT>
Your function is now deployed and ready to be used.