Serverless Devs は、サーバーレスプロジェクトのライフサイクル全体にわたる開発、ビルド、テスト、デプロイを行うためのオープンソースツールです。このトピックでは、Serverless Devs を使用して Function Compute プロジェクトを初期化し、デプロイする方法について説明します。すべての例で Node.js 14 を使用します。
コマンドクイックリファレンス
| コマンド | 説明 |
|---|---|
sudo s | 対話型プロジェクトウィザードを開始します (メソッド 1) |
sudo s init <template> | 特定のテンプレートからプロジェクトを初期化します (メソッド 2) |
sudo s build | プロジェクトをローカルでビルドします |
sudo s local start | HTTP 関数のためのローカルサーバーを起動します |
sudo s local invoke | イベント関数をローカルで呼び出します |
sudo s deploy -y | プロジェクトを Function Compute にデプロイします |
sudo s invoke -e '<payload>' | デプロイされた関数をリモートで呼び出します |
sudo s remove service | サービスと関連するすべてのリソースを削除します |
前提条件
開始する前に、以下の前提条件を満たしていることを確認してください:
Function Compute サービスがアクティベートされていること。詳細については、「Function Compute のアクティベート」をご参照ください。
メソッドの選択
| メソッド | コマンド | 最適な用途 | 得られるもの |
|---|---|---|---|
| メソッド 1:対話型セットアップ | s | 初めてのユーザー | プロジェクト作成とワンステップデプロイのためのガイド付きウィザード |
| メソッド 2:ステップバイステップのワークフロー | s init | 詳細なコントロール | ビルド、ローカルデバッグ、リモート呼び出し、リソースの削除 |
方法 1: s によるインタラクティブなセットアップ
対話型ウィザードを開始するには、s を実行します。
sudo sウィザードのプロンプトに従ってプロジェクトを設定します。以下は、典型的なセッションの例です:
No Serverless-Devs project is currently detected. Do you want to create a new project? Yes
Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md
Hello Serverless for Cloud Vendors Alibaba Cloud Serverless
Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]
Which template do you like? [Event] Node.js 14
Create application command: [s init devsapp/start-fc-event-nodejs14]
Please input your project name (init dir) start-fc-event-nodejs14
file decompression completed
____ _ _ ___ _ _ _ _____ ____
/ _ \/ \ / \\ \/// \ /\/ \ /| / // _\
| / \|| | | | \ / | | ||| |\ || | __\| /
| |-||| |_/\| | / / | \_/|| | \|| | | | \__
\_/ \|\____/\_//_/ \____/\_/ \| \_/ \____/
please select credential alias default
Welcome to the Aliyun FC start application
This application requires to open these services:
FC : https://fc.console.alibabacloud.com/
* The project is initialized. You can go to the project directory and run the s deploy command to deploy the project.
Thanks for using Serverless-Devs
You could [cd /test/start-fc-event-nodejs14] 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
Do you want to deploy the project immediately? Yes
......
helloworld:
region: cn-hangzhou
service:
name: hello-world-service
function:
name: event-nodejs14
runtime: nodejs14
handler: index.handler
memorySize: 128
timeout: 60対話型フローでの主な選択肢:
クラウドベンダー: [Alibaba Cloud Serverless] を選択してください。
[テンプレート]: 関数タイプとランタイムを選択します。たとえば、[イベント] Node.js 14 です。
[プロジェクト名]:デフォルト値を受け入れるか、カスタム名を入力します。
[認証情報エイリアス]:Serverless Devs のセットアップ時に設定したエイリアスを選択します。
[即時デプロイ]: [はい] を選択するとワンステップでデプロイされ、[いいえ] を選択すると後でデプロイできます。
方法 2: s init を使用したステップごとのワークフロー
プロジェクトの初期化
sudo s init devsapp/start-fc-http-nodejs14この例では、Node.js 14 を使用した HTTP 関数をデプロイします。httpをeventなどの別のトリガーの種類に置き換えると、関数の種類を変更できます。nodejs14を別のランタイム識別子に置き換えると、ランタイムを変更できます。
出力例:
Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md
Please input your project name (init dir) start-fc-http-nodejs14
file decompression completed
____ _ _ ___ _ _ _ _____ ____
/ _ \/ \ / \\ \/// \ /\/ \ /| / // _\
| / \|| | | | \ / | | ||| |\ || | __\| /
| |-||| |_/\| | / / | \_/|| | \|| | | | \__
\_/ \|\____/\_//_/ \____/\_/ \| \_/ \____/
please select credential alias default
Welcome to the Aliyun FC start application
This application requires to open these services:
FC : https://fc.console.alibabacloud.com/
* The project is initialized. You can go to the project directory and run the s deploy command to deploy the project.
Thanks for using Serverless-Devs
You could [cd /test/start-fc-http-nodejs14] 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
Do you want to deploy the project immediately? No初期化後、プロジェクトディレクトリには以下のファイルが含まれます:
| ファイル | 説明 |
|---|---|
s.yaml | Function Compute のリソースを定義します。詳細については、「YAML 構文と権限管理」をご参照ください。 |
s_en.yaml | s.yaml と同じ構成が含まれています。デフォルトでは、システムは s.yaml を使用します。代わりに s_en.yaml を使用するには、-t パラメーターを指定します: s deploy -t s_en.yaml -y。 |
code/ | 関数ロジックを含む index.js が含まれています。 |
readme.md | 依存関係、モジュール、およびプロジェクト構成を記述します。 |
プロジェクトディレクトリへの移動
cd start-fc-http-nodejs14プロジェクトのビルド (任意)
s build コマンドを実行して、プロジェクトをローカルでコンパイルおよびパッケージ化します:
sudo s buildこれにより、デバッグおよびデプロイメント用のビルド アーティファクトを格納する .s ディレクトリがプロジェクトのルートに作成されます。
ローカルでのデバッグ (任意)
ビルド後、デプロイする前にお使いのマシンで関数をテストします。
HTTP 関数:
sudo s local start出力例:
[2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - Using trigger for start: name: httpTrigger
type: http
config:
authType: anonymous
methods:
- GET
[2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of hello-world-service/http-trigger-nodejs14 was registered
url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
methods: GET
authType: anonymous
Tips: You can also use these commands to run or debug custom domain resources:
Start with customDomain:
* s local start auto
Debug with customDomain:
* s local start -d 3000 auto
Tips for next step
======================
* Deploy Resources: s deploy
helloworld:
status: succeed
function compute app listening on port 7013!cURL またはブラウザを使用して HTTP 関数をトリガーします:
curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/または、ブラウザで次の URL を開きます:
http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/イベント関数:
sudo s local invokeコマンドの実行直後に結果が返されます。
Function Compute へのデプロイ
sudo s deploy -y出力例:
Checking Service, Function, Triggers (0.82s)
Creating Service, Function, Triggers (0.6s)
Creating custom domain (0.31s)
Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain
helloworld:
region: cn-hangzhou
service:
name: hello-world-service
function:
name: http-trigger-nodejs14
runtime: nodejs14
handler: index.handler
memorySize: 128
timeout: 60
url:
custom_domain:
-
domain: http://http-trigger-nodejs14.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net
triggers:
-
type: http
name: httpTrigger関数のリモート呼び出し (任意)
デプロイ後、クラウドで関数を呼び出して動作を検証します:
sudo s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'出力例:
Request url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c
load code for handler:index.handler
2022-03-04T08:39:14.770Z 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c [verbose] hello world
FC Invoke End RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c
Duration: 66.23 ms, Billed Duration: 67 ms, Memory Size: 128 MB, Max Memory Used: 42.48 MB
========= FC invoke Logs end =========
FC Invoke Result[Code: 200]:
{
path: '//',
queries: { key: 'value' },
headers: {
accept: 'application/json',
authorization: 'FC yourAccessKeyID:yourAccessKeySecret',
date: 'Fri, 04 Mar 2022 08:39:14 GMT',
header_1: 'v1',
host: '188077086902****.cn-hangzhou.fc.aliyuncs.com',
'user-agent': 'Node.js(v14.17.4) OS(linux/x64) SDK(@alicloud/fc2@v2.5.0)',
'x-forwarded-proto': 'http'
},
method: 'GET',
requestURI: '/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14//?key=value',
clientIP: '47.97.XX.XX',
body: ''
}
End of method: invokecURL を使用して HTTP 関数をトリガーすることもできます:
curl https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/リソースの削除 (任意)
デプロイされたリソースが不要になった場合は、削除します:
sudo s remove service出力例:
Need to delete the resource in the cn-hangzhou area, the operation service is hello-world-service:
......
Delete trigger hello-world-service/http-trigger-nodejs14/httpTrigger success.
Delete function hello-world-service/http-trigger-nodejs14 success.
Delete service hello-world-service success.
[2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service
End of method: removeこのコマンドは、トリガー、関数、サービスを順番に削除し、関連する NAS サービスリソースをクリーンアップします。