Serverless Devs は、サーバーレスアプリケーション向けのオープンソース開発者ツールです。プロジェクトのライフサイクル全体を管理し、関数の迅速な作成、デプロイ、呼び出しを可能にします。このトピックでは、Python ランタイムを持つ関数を例に、Serverless Devs を使用して関数を管理する方法について説明します。
操作手順
ステップ 1: Serverless Devs のインストール
Serverless Devs は、コマンドラインインターフェイスの使用、バイナリファイルのダウンロード、またはスクリプトの実行によってインストールできます。このトピックでは、npm パッケージ管理ツールを使用したインストールを例に、その手順を説明します。
Node.js と npm パッケージ管理ツールをインストールします。
インストール手順については、Node.js の公式サイトをご参照ください。
説明Node.js はバージョン 14 以降である必要があります。インストール後、
node --versionコマンドを実行して Node.js のバージョンを確認できます。次のコマンドを実行して Serverless Devs をインストールします。
sudo npm install @serverless-devs/s -g次のコマンドを実行してインストールを検証します。
sudo s -v以下は、インストールが成功した場合の出力例です。
@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4
ステップ 2: Serverless Devs の設定
Alibaba Cloud キーを取得します。
AccessKey を作成する際は、必ず AccessKey Secret を保存してください。詳細については、「AccessKey の作成」をご参照ください。
説明AccessKey ID と AccessKey Secret は、Alibaba Cloud API にアクセスするための認証情報です。これらのキーは、アカウントに対する完全な権限を付与します。安全に保管する必要があります。
ガイド付きの手順に従ってキーを設定します。
次のコマンドを実行してクラウドベンダーを選択します。
sudo s config add出力:
> 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)目的のクラウドベンダーを選択し、Enter キーを押します。このトピックでは、
Alibaba Cloud (alibaba)を例として使用します。対話形式のプロンプトに従って、キー情報を設定します。
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
ステップ 3: サンプル関数の初期化
次のコマンドを実行して、Python で Hello World プロジェクトを作成します。
sudo s init start-fc3-python出力は次のとおりです。
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プロジェクトが初期化されると、現在のディレクトリに start-fc3-python フォルダが作成されます。このフォルダには、次のファイルが含まれています。
s.yaml: Function Compute のリソース定義ファイルです。このファイルは YAML フォーマットで記述されています。
code: code フォルダには index.py ファイルが含まれています。このファイルで関数のコードロジックを編集できます。
readme.md: プロジェクトに必要なモジュールと設定情報を定義するマニフェストファイルです。
次のコマンドを実行して、プロジェクトフォルダに移動します。
cd start-fc3-python
ステップ 4: 関数のデプロイ
次のコマンドを実行して、関数を Function Compute にデプロイします。関数名、ランタイム、メモリなどの関数のメタデータは、s.yaml ファイルで定義されます。
sudo s deploy出力は次のとおりです。
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ステップ 5: 関数の呼び出し
次のコマンドを実行して関数を呼び出します。
sudo s invoke -e "test"出力は次のとおりです。
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ステップ 6: 関数の削除 (任意)
関数を使用する必要がなくなった場合は、次のコマンドを実行して削除できます。
sudo s remove出力は次のとおりです。
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