Serverless Devsは、サーバーレスアプリケーション開発者向けのオープンソースプラットフォームです。 Serverless Devsを使用して、プロジェクトの開発、作成、テスト、デプロイを行い、プロジェクトのライフサイクルを効率的に管理できます。 このトピックでは、Serverless Devsを使用してFunction Computeアプリケーションテンプレートをダウンロードし、そのテンプレートを使用して関数アプリケーションを開発およびデプロイする方法について説明します。 このトピックでは、Node.js 12の関数を使用します。

このタスクについて

このトピックでは、Serverless Devsでプロジェクトを初期化する2つの方法について説明します。 Serverless Devsを初めて使用する場合は、方法1を使用することを推奨します。 Serverless Devsでローカルデバッグ、リモート呼び出し、リソース削除などの操作を実行する方法について詳しく知りたい場合は、方法2を使用することをお勧めします。

作業の前に

  1. Serverless DevsとDockerのインストール
  2. Serverless Devsの設定

方法1: sコマンドを使用してリソースを管理する

次のコマンドを実行して、プロジェクトを初期化します。
s
コマンド出力に基づいてプロジェクトを初期化します。 次の出力が返されます。
 No Serverless-Devs project is currently detected. Do you want to create a new project? Yes//在项目文件内未检测到Serverless-Devs项目。选择是否创建一个项目。

 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]//选择您需要创建的模板。本示例以快速部署运行环境为Node.js 12的事件函数为例。
 Which template do you like? [Event] Node.js 12

 Create application command: [s init devsapp/start-fc-event-nodejs12]

 Please input your project name (init dir) start-fc-event-nodejs12//Serverless Devs默认为您生成一个项目名称,您也可以自定义该名称。
 file decompression completed

     ____  _     _ ___  _ _     _        _____ ____
    /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
    | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
    | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
    \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
 please select credential alias default//选择您在配置Serverless Devs时设置的别名。

    Welcome to the Aliyun FC start application
     This application requires to open these services:
         FC : https://fc.console.aliyun.com/

     * 项目初始化完成,您可以直接进入项目目录下,并使用s deploy进行项目部署。

 Thanks for using Serverless-Devs
 You could [cd /test/start-fc-event-nodejs12] 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-nodejs12
    runtime:    nodejs12
    handler:    index.handler
    memorySize: 128
    timeout:    60

方法2: s initコマンドを使用してリソースを管理する

  1. プロジェクトを初期化します。
    プロジェクトディレクトリで次のコマンドを実行して、プロジェクトを初期化します。
    s init devsapp/start-fc-http-nodejs12
    この例では、Node.js 12のHTTP関数を展開して、プロジェクトを初期化する方法を説明します。 別のランタイム環境に他の種類の関数を展開する場合は、httpeventなどの使用するトリガーの種類に置き換えます。 nodejs12を別のランタイム環境に置き換えることもできます。
    次の出力が返されます。
      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-nodejs12//Serverless Devs会默认生成一个项目名称,您也可以自定义该名称。
     file decompression completed
    
         ____  _     _ ___  _ _     _        _____ ____
        /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
        | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
        | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
        \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
     please select credential alias default//选择您在配置Serverless Devs时设置的别名。
    
        Welcome to the Aliyun FC start application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
    
         * 项目初始化完成,您可以直接进入项目目录下,并使用s deploy进行项目部署。
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-fc-http-nodejs12] 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//选择部署项目。
    プロジェクトが初期化されると、start-fc-http-nodejs12という名前のファイルがディレクトリに生成されます。 start-fc-http-nodejs12ファイルには, 次のファイルが含まれます。
    • s.yaml: Function Computeリソースを定義するYAMLファイル。 YAML形式の詳細については、「YAML構文と権限管理」をご参照ください。
    • code: codeフォルダーにindex.jsファイルが含まれます。 このファイル内の関数のコードロジックを編集できます。
    • readme.md: 依存モジュールやプロジェクトの設定など、依存関係を定義するファイル。
  2. 次のコマンドを実行して、プロジェクトディレクトリに移動します。
    cd start-fc-http-nodejs12
  3. オプション: アプリケーションを作成します。
    プロジェクトディレクトリで次のコマンドを実行して、アプリケーションを作成します。
    s build
    この手順を実行した後、ファイルは、出力を格納するために現在のディレクトリに生成されます。 このファイルを使用して、コンピューターにアプリケーションをデバッグおよび展開できます。
  4. オプション: コンピューターでアプリケーションをデバッグします。
    アプリケーションをビルドした後、コンピューターでアプリケーションをデバッグして、アプリケーションが期待どおりに実行されるかどうかを確認できます。 関数の種類に基づいて、次のいずれかのコマンドを実行します。
    • HTTP関数
      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-nodejs12 was registered
              url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
              methods: GET
              authType: anonymous
      
      Tips:you can also use these commands to run/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!
    • イベント関数
      s local invoke
    実行結果は関数の種類によって異なります。
    • HTTP関数
      HTTP関数をデバッグする場合、デバッグコマンドの実行後にURLが返され、デバッグが停止します。 次のいずれかの方法を使用して、関数の実行をトリガーできます。
      • cURLを使用してHTTP関数をトリガーします。
        curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
      • ブラウザを使用してHTTP機能をトリガーします。
        http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
    • イベント関数

      イベント関数をデバッグする場合、デバッグコマンドの実行後にデバッグ結果が返されます。

  5. アプリケーションをデプロイします。
    次のコマンドを実行して、アプリケーションをFunction Computeにデプロイします。
    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-nodejs12
        runtime:    nodejs12
        handler:    index.handler
        memorySize: 128
        timeout:    60
      url:
        custom_domain:
          -
            domain: http://http-trigger-nodejs12.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        -
          type: http
          name: httpTrigger
    実行が成功すると、アプリケーションはFunction Computeにデプロイされます。
  6. オプション: リモートでアプリケーションをデバッグします。
    次のコマンドを実行して、アプリケーションをリモートデバッグします。
    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-nodejs12/
    ========= 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 LTAI4G4cwJkK4Rza6xd9****:KZsoanKle4SoRO8EjyOjY3duLlVlR1mp78hjV9WG****',
        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-nodejs12//?key=value',
      clientIP: '47.97.XX.XX',
      body: ''
    }
    
    End of method: invoke
    この例では、HTTP関数がデプロイされています。 リモートデバッグコマンドを実行すると、URLが返されます。 cURLを使用して、function ComputeでHTTP関数をトリガーできます。
    curl https:// 188077086902 **** .cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
  7. オプション: アプリケーションを削除します。
    次のコマンドを実行して、アプリケーションを削除します。
    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-nodejs12/httpTrigger success.
     Delete function hello-world-service/http-trigger-nodejs12 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
    実行が成功すると、アプリケーションは削除されます。