O Serverless Devs é uma CLI serverless de código aberto. Utilize esta ferramenta para implantar uma função Python no Function Compute, invocar a função e limpar os recursos.
Pré-requisitos
Certifique-se de ter:
Uma conta Alibaba Cloud com um par de AccessKey. Salve o AccessKey secret durante a criação. Crie um par de AccessKey
Node.js 14 ou superior. Baixe no site do Node.js
Verifique sua versão do Node.js:
node --version
Etapa 1: Instale o Serverless Devs
É possível instalar o Serverless Devs via npm, download de binário ou script. Este guia utiliza o npm.
sudo npm install @serverless-devs/s -g
Verifique a instalação:
sudo s -v
Saída esperada:
@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4
Etapa 2: Configure seu par de AccessKey
Seu AccessKeyID e AccessKeySecret concedem acesso total à sua conta Alibaba Cloud. Mantenha-os seguros.
-
Inicie o assistente de configuração:
sudo s config addA CLI lista os provedores de nuvem suportados:
> 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) Selecione Alibaba Cloud (alibaba) e pressione Enter.
-
Insira seu par de AccessKey quando solicitado.
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
Etapa 3: Inicializar uma função de exemplo
-
Crie um projeto Hello World em Python:
sudo s init start-fc3-pythonSiga as instruções para selecionar uma região, nome da função e 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.comIsso cria uma pasta start-fc3-python com os seguintes arquivos:
Arquivo Descrição s.yaml Definição de recurso para o Function Compute no formato YAML. code/index.py Ponto de entrada do código da função readme.md Define módulos e configurações do projeto -
Acesse a pasta do projeto:
cd start-fc3-python
Etapa 4: Implantar a função
Implante a função. Metadados como nome, runtime e memória são definidos em s.yaml.
sudo s deploy
Saída esperada:
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
Verifique se a saída exibe o nome da sua função e a região.
Etapa 5: Invocar a função
Envie um evento de teste para a função:
sudo s invoke -e "test"
Saída esperada:
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
O campo Invoke Result exibe test, confirmando que a função processou a entrada.
Etapa 6: Limpeza (opcional)
Exclua a função quando não precisar mais dela:
sudo s remove
Selecione yes quando solicitado para confirmar a exclusão.
Saída esperada:
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