Get started with Function Compute using Serverless Devs

更新时间:
复制 MD 格式

Serverless Devs is an open-source serverless CLI. Use it to deploy a Python function to Function Compute, invoke the function, and clean up.

Prerequisites

Make sure you have:

Verify your Node.js version:

node --version

Step 1: Install Serverless Devs

Serverless Devs can be installed via npm, binary download, or script. This guide uses npm. Other methods: Install Serverless Devs.

sudo npm install @serverless-devs/s -g

Verify the installation:

sudo s -v

Expected output:

@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4

Step 2: Configure your AccessKey pair

Note

Your AccessKey ID and AccessKey secret grant full access to your Alibaba Cloud account. Keep them secure.

  1. Start the configuration wizard:

    sudo s config add

    The CLI lists supported cloud providers:

    > 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)
  2. Select Alibaba Cloud (alibaba) and press Enter.

  3. Enter your AccessKey pair when prompted. Other key configuration methods: Configure Serverless Devs.

    Please select a provider: Alibaba Cloud (alibaba)
     Refer to the document for alibaba key:  http://config.devsapp.net/account/alibaba
     AccountID 188077086902****
     AccessKeyID yourAccessKeyID
     AccessKeySecret yourAccessKeySecret
     Please create alias for key pair. If not, please enter to skip default
    
        Alias: default
        AccountID: 188077086902****
        AccessKeyID: yourAccessKeyID
        AccessKeySecret: yourAccessKeySecret
    
    Configuration successful

Step 3: Initialize a sample function

  1. Create a Hello World project in Python:

    sudo s init start-fc3-python

    Follow the prompts to select a region, function name, and runtime:

    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
    
    ...
    Region where the application is created
    ? Region cn-huhehaote
    Must contain only letters, digits, underscores (_), and hyphens (-). Cannot start with a digit or a hyphen. The length must be 1 to 128 characters.
    ? Function name start-python-8i32
    Region where the application is created
    ? python runtime python3.9
    ? 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

    This creates a start-fc3-python folder with these files:

    File Description
    s.yaml Resource definition for Function Compute in YAML format. YAML Specification.
    code/index.py Function code entry point
    readme.md Defines project modules and configuration
  2. Navigate to the project folder:

    cd start-fc3-python

Step 4: Deploy the function

Deploy the function. Metadata such as name, runtime, and memory is defined in s.yaml.

sudo s deploy

Expected output:

   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

Verify the output shows your function name and region.

Step 5: Invoke the function

Send a test event to the function:

sudo s invoke -e "test"

Expected output:

   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

Invoke Result shows test, confirming the function processed the input.

Step 6: Clean up (optional)

Delete the function when you no longer need it:

sudo s remove

Select yes when prompted to confirm deletion.

Expected output:

   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

More information

Troubleshoot common Serverless Devs issues: Tips.