Serverless Devs is an open source serverless developer tool. Serverless Devs allows you to create, deploy, and invoke functions in an efficient manner and manage projects throughout their lifecycles. This topic describes how to use Serverless Devs to manage functions that are deployed in Python.
Procedure
Step 1: Install Serverless Devs
You can install Serverless Devs by using CLI, downloading binary files, and scripts. This topic describes how to install Serverless Devs by using Node Package Manager (npm).
Install Node.js and npm.
For more information, visit Download Node.js.
NoteThe version of Node.js must be Node.js 14 or later. After the installation, you can run the
node --version
command to view the version of Node.js.Run the following command to install Serverless Devs:
sudo npm install @serverless-devs/s -g
Run the following command to check whether the installation is complete:
sudo s -v
The following sample code is returned if Serverless Devs is successfully installed.
@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4
Step 2: Configure Serverless Devs
Obtain the Alibaba Cloud AccessKey information.
You must save the AccessKey secret when you create an AccessKey pair. For more information, see Create an AccessKey pair.
NoteThe AccessKey ID and AccessKey secret of your Alibaba Cloud account are the credentials to access Alibaba Cloud APIs. Make sure that your AccessKey pair is safe.
Configure the key as prompted.
Run the following command to query the cloud service providers that can be selected:
sudo s config add
The following output is returned:
> Alibaba Cloud (alibaba) AWS (aws) Azure (azure) Baidu Cloud (baidu) Google Cloud (google) Huawei Cloud (huawei) Tencent Cloud (tencent) (Move up and down to reveal more choices)
Select a cloud service provider and press the Enter key. In this example,
Alibaba Cloud (alibaba)
is selected.Configure the key as prompted.
Please select a provider: Alibaba Cloud (alibaba) Refer to the document for alibaba key: http://config.devsapp.net/account/alibaba AccountID 188077086902**** AccessKeyID LTAI4G4cwJkK4Rza6xd9**** AccessKeySecret eCc0GxSpzfq1DVspnqqd6nmYNN**** Please create alias for key pair. If not, please enter to skip default Alias: default AccountID: 188077086902**** AccessKeyID: LTAI4G4cwJkK4Rza6xd9**** AccessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** Configuration successful
Step 3: Initialize a function
Run the following command to create a Hello World project in Python:
sudo s init start-fc3-python
The following output is returned:
More applications: https://registry.serverless-devs.com ? Please input your project name (init dir) start-fc3-python Downloading[/v3/packages/start-fc3-python/zipball/0.0.8]... Download start-fc3-python successfully ... The region where the application is created. ? Example: cn-huhehaote. The name can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or hyphen (-). It must be 1 to 128 characters in length. ? Function name: start-python-8i32. The region where the application is created. ? Specify the Python runtime. In this example, python3.9 is used. ? please select credential alias default * Before using, please check whether the actions command in Yaml file is available * Carefully reading the notes in s.yaml is helpful for the use of the tool * If need help in the use process, please apply to join the Dingtalk Group: 33947367 Thanks for using Serverless-Devs You could [cd /start-fc3-python] and enjoy your serverless journey! If you need help for this example, you can use [s -h] after you enter folder. Document❤Star: https://github.com/Serverless-Devs/Serverless-Devs More applications: https://registry.serverless-devs.com
After the project is initialized, the start-fc3-python folder is generated in the current directory that contains the following files:
s.yaml: The Function Compute resource definition file, in YAML syntax.
code: The code folder contains the index.py file, in which you can edit the code logic.
readme.md: The file that defines the dependencies, such as dependent modules and the configurations of the project.
Run the following command to go to the project directory:
cd start-fc3-python
Step 4: Deploy the function
Run the following command to deploy the function to Function Compute: The function metadata, such as the name, runtime, and memory, is defined in the s.yaml
file.
sudo s deploy
The following output is returned:
Steps for [deploy] of [hello-world-app]
====================
✔ [hello_world] completed (3.1s)
Result for [deploy] of [hello-world-app]
====================
region: cn-huhehaote
description: hello world by serverless devs
functionName: start-python-8i32
handler: index.handler
internetAccess: true
memorySize: 128
role:
runtime: python3.9
timeout: 30
A complete log of this run can be found in: /Users/x1/.s/logs/0229164849
Step 5: Invoke the function
Run the following command to invoke the function:
sudo s invoke -e "test"
The following output is returned:
Steps for [invoke] of [hello-world-app]
====================
========= FC invoke Logs begin =========
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: 1-65e045b1-123e1745-79e146cd8b01
2024-02-29T08:52:01.317Z 1-65e045b1-123e1745-79e146cd8b01 [INFO] b'test'
FC Invoke End RequestId: 1-65e045b1-123e1745-79e146cd8b01
Duration: 2.38 ms, Billed Duration: 3 ms, Memory Size: 128 MB, Max Memory Used: 8.78 MB
========= FC invoke Logs end =========
Invoke instanceId: c-65e045b1-12f566fe-ca21d8fe46f1
Code Checksum: 3418420950487476908
Qualifier: LATEST
RequestId: 1-65e045b1-123e1745-79e146cd8b01
Invoke Result:
test
✔ [hello_world] completed (0.46s)
A complete log of this run can be found in: /Users/x1/.s/logs/0229165200
Step 6: Delete a function (optional)
If you no longer want to use the function, run the following command to delete the function:
sudo s remove
The following output is returned:
Steps for [remove] of [hello-world-app]
====================
Remove function: cn-huhehaote/start-python-8i32
? Are you sure you want to delete the resources listed above yes
✔ [hello_world] completed (11.67s)
A complete log of this run can be found in: /Users/x1/.s/logs/0229165500