Function Compute の組み込みランタイムがビジネス要件を満たさない場合、カスタムコンテナ関数を利用できます。本トピックでは、Function Compute コンソールまたは Serverless Devs を使用してカスタムコンテナ関数を作成する方法について説明します。
注意事項
Function Compute でカスタムコンテナ関数を作成するには、同一リージョン・同一アカウント内の Alibaba Cloud Container Registry リポジトリからイメージを使用する必要があります。Apple 製チップを搭載した Mac コンピューター、またはその他の ARM アーキテクチャのマシンでイメージをビルドする場合は、イメージのビルドプラットフォームを Linux/amd64 として指定する必要があります。例: docker build --platform linux/amd64 -t $IMAGE_NAME .。
前提条件
Container Registry
- 説明
Personal Edition または Enterprise Edition のインスタンスを作成できます。Enterprise Edition インスタンスの利用を推奨します。
Container Registry (ACR) Personal Edition は個人開発者向けのサービスです。パブリックプレビュー期間中は無料トライアルとして利用可能ですが、クォータ制限があります。また、サービスレベルアグリーメント (SLA) や損害賠償の保証はなく、利用制限が適用されます。制限内容の詳細については、「Personal Edition インスタンスの作成に関する注意事項」をご参照ください。
Serverless Devs
コンソールでの関数作成
ステップ 1:サービスの作成と権限付与
Function Computeコンソールでサービスを作成します。
詳細については、「サービスの作成」をご参照ください。
対象サービスに AliyunContainerRegistryReadOnlyAccess または AliyunContainerRegistryFullAccess のアクセスポリシーをアタッチします。
詳細については、「Function Compute による他 Alibaba Cloud サービスへのアクセス権限の付与」をご参照ください。Function Compute は、Container Registry のデフォルトインスタンスから一時的なアカウント情報を取得するために、指定されたポリシーに含まれる権限を必要とします。その後、この一時アカウントを使用して、ご利用のプライベートイメージリポジトリからイメージをプッシュします。
ステップ 2:関数の作成
Function Compute コンソールにログインします。左側のナビゲーションウィンドウで、[サービスと関数] をクリックします。
上部のナビゲーションバーでリージョンを選択します。[サービス] ページで、対象のサービスをクリックします。
- [関数] ページで、[関数の作成] をクリックします。
[関数の作成] ページで、コンテナイメージを使い作成する を選択し、必要に応じて以下のパラメーターを構成した後、[作成] をクリックします。
基本設定:関数名やハンドラータイプなど、関数の基本情報を指定します。
設定項目
説明
関数名
任意の関数名を入力します。空欄のままにすると、Function Compute が自動的に生成します。
ハンドラータイプ
関数のタイプを選択します。これにより、関数がイベントリクエストまたは HTTP リクエストを処理するかが決定されます。以下のオプションが利用可能です:
イベントリクエストを処理:タイマー、API または SDK 呼び出し、または他の Alibaba Cloud サービスからのトリガーによって関数が起動されます。
HTTP リクエストを処理:関数が HTTP または WebSocket リクエストを処理します。Web シナリオでは、カスタムランタイムを使い作成する を推奨します。
イメージ構成:関数作成に使用するイメージを選択します。
設定項目
説明
イメージ選択方法
関数を作成するために、[サンプルイメージを使用] するか、ご自身のイメージを使用できます。
[サンプルイメージを使用]: Function Compute が提供するサンプルイメージを選択します。
[ACR からのイメージを使用]: [コンテナイメージ] パラメーターの下にある [ACR からイメージを選択] をクリックします。[コンテナイメージの選択] パネルが表示されたら、[コンテナインスタンス] および [ACR イメージリポジトリ] を選択します。次に、イメージ一覧から対象のイメージを見つけ、[選択] をクリックします([操作] 列)。
説明他のアカウントが所有する ACR リポジトリのパブリックイメージは使用できません。
Container Registry Enterprise Edition の場合、Function Compute は、選択したイメージのタグを使用して関数インスタンスを作成します。
Enterprise Edition イメージ(Premium Edition または Standard Edition)を使用する場合は、[不変] 設定を [イメージバージョン] に対して有効化することを推奨します。そうでない場合、他の場所でイメージタグが更新されると、Function Compute が最新のイメージデータをプルしてインスタンスを起動します。「イメージバージョンの不変化の有効化」をご参照ください。
Enterprise Edition イメージ(Basic Edition)または ACR Personal Edition を使用する場合は、イメージリポジトリ内で同名のイメージタグを更新した後、Function Compute コンソールで関数を新規イメージ情報とともに更新する必要があります。その後、Function Compute は新しいイメージを使用してインスタンスを起動します。
起動コマンド
コンテナの起動コマンドです。空欄のままにすると、イメージの Entrypoint または CMD がデフォルトで使用されます。
リスニングポート
コンテナイメージ内の HTTP サーバーがリスンするポートです。デフォルトポートは 9000 です。
高度な設定:インスタンス関連の設定および関数の実行タイムアウト期間を構成します。
設定項目
説明
GPU を使用しますか?
ビジネス要件に応じて、GPU 加速インスタンスを利用するかどうかを選択します。デフォルトでは CPU インスタンスが使用され、GPU 加速インスタンスは使用されません。「インスタントタイプ」をご参照ください。各種インスタンスタイプの課金についての詳細は、「課金概要」をご参照ください。
[GPU を使用]
[GPU を使用しない]
インスタンスの仕様
[GPU を使用] を選択します。
[GPU カードタイプ] を選択します。その後、ビジネス要件に応じて適切な [GPU メモリ] の仕様を選択します。Function Compute は、選択した GPU に基づいて [vCPU] および [メモリ] の仕様を自動的に選択します。柔軟な比率はサポートされていません。
[GPU を使用しない] を選択します。
ビジネス要件に応じて、適切な [vCPU] および [メモリ] の仕様の組み合わせを選択するか、手動で入力します。
各リソースの課金についての詳細は、「課金概要」をご参照ください。
説明vCPU(コア数)とメモリ(GB)の比率は、1:1 ~ 1:4 の範囲内に設定する必要があります。
一時ディスクサイズ
ビジネス要件に応じてディスクサイズを選択します。Function Compute は、最大 512 MB のディスク使用量について無料クォータを提供しています。「課金概要」をご参照ください。
インスタンス同時実行数
関数インスタンスの同時実行数を設定します。「インスタンス同時実行数の設定」をご参照ください。
実行タイムアウト
タイムアウト期間を設定します。デフォルト値は 60 秒です。最大値は 86400 秒です。このパラメーターは 600 秒に設定することを推奨します。
タイムゾーン
関数のタイムゾーンを選択します。タイムゾーンを設定すると、[TZ] という環境変数が関数に自動的に追加されます。この変数の値は、設定したタイムゾーンとなります。
環境変数:関数の実行環境に環境変数を設定します。「環境変数」をご参照ください。
トリガー構成:関数を実行するトリガーを構成します。「トリガーの管理」をご参照ください。
関数が作成された後、対象サービスの関数一覧で関数を表示および更新できます。
関数を更新する際には、構成済みのリスニングポートのみ変更できます。ポートを削除したり、他のリスニングポートを追加したりすることはできません。関数作成時にリスニングポートを構成した場合、更新時にリスニングポートを指定しなかった場合でも、そのポートは保持されます。
Serverless Devs を使用した関数作成
Serverless Devs を使用すると、コンテナイメージのビルド、リポジトリへのプッシュ、および関数のデプロイを 1 ステップで実行できます。
以下のコマンドを実行してプロジェクトを初期化します。プロンプトに従い、ACR イメージリポジトリのアドレスを入力します。
sudo s init start-fc-custom-container-event-nodejs14実行例の出力:
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome Please input your project name (init dir) start-fc-custom-container-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/ ACR: https://cr.console.alibabacloud.com/ * Note: The actions are declared in s.yaml: Pre-deployment execution: sudo s build --use-docker --dockerfile ./code/Dockerfile If you do not need to build the project every time, do not need to build it before deployment, or have already built it manually, you can comment out this part. > PS: You also need to modify the image field in s.yaml to the address of your ACR configuration during deployment. * The project is initialized. You can go to the project directory and use s deploy to deploy the project. Thanks for using Serverless-Devs You could [cd /test/test1/start-fc-custom-container-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? No以下のコマンドを実行してプロジェクトディレクトリに移動します。
cd start-fc-custom-container-event-nodejs14s.yamlファイルを編集します。YAML ファイル内のパラメーターについての詳細は、「YAML 仕様」をご参照ください。以下のコマンドを実行してプロジェクトをデプロイします。
sudo s deploy実行例の出力:
[2021-12-15 07:54:30] [INFO] [S-CLI] - Start ... [2021-12-15 07:54:30] [INFO] [S-CLI] - Start the pre-action [2021-12-15 07:54:30] [INFO] [S-CLI] - Action: s build --use-docker --dockerfile ./code/Dockerfile [2021-12-15 07:54:31] [INFO] [S-CLI] - Start ... [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Build artifact start... [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Use docker for building. [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Building image... Sending build context to Docker daemon 5.12kB Step 1/7 : FROM node:14.5.0-alpine3.11 ---> 5d97b3d11dc1 ...... Step 7/7 : ENTRYPOINT [ "node", "server.js" ] ---> Using cache ---> a5ef1c015e7e Successfully built a5ef1c015e7e Successfully tagged registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. Build image(registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx) successfully [2021-12-15 07:54:33] [INFO] [FC-BUILD] - Build artifact successfully. Tips for next step ====================== * Invoke Event Function: s local invoke * Invoke Http Function: s local start * Deploy Resources: s deploy End of method: build [2021-12-15 07:54:33] [INFO] [S-CLI] - End the pre-action [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using access alias: default [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeyID: yourAccessKeyID [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: yourAccessKeySecret [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole [2021-12-15 07:54:35] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists [2021-12-15 07:54:35] [INFO] [RAM] - Updating role: AliyunFCDefaultRole [2021-12-15 07:54:35] [INFO] [RAM] - Checking Policy AliyunFCDefaultRolePolicy exists [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Using image registry: registry.cn-hangzhou.aliyuncs.com [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Try to use a temporary token for docker login Login to registry: registry.cn-hangzhou.aliyuncs.com with user: cr_temp_user Pushing docker image: registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx... The push refers to repository [registry.cn-hangzhou.aliyuncs.com/fc-example/test] cdf38e7753b7: Layer already exists 43128f71725b: Layer already exists 0fb36a16ab83: Layer already exists dd966b9fd474: Layer already exists a1915d7a1111: Layer already exists c4491b3ee709: Layer already exists 9fb10d900487: Layer already exists 3e207b409db3: Layer already exists nginx: digest: sha256:02b69157def85ceb72f32cb1c5845d00e1d8df19caf6eaf720a9bc77bb57db76 size: 1991 √ Make service hello-world-service success. √ Make function hello-world-service/nodejs14-event-function success. [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists There is auto config in the service: hello-world-service 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: nodejs14-event-function runtime: custom-container handler: not-used memorySize: 256 timeout: 60以下のコマンドを実行して関数をテストします。
sudo s invoke -e "{\"key\":\"val\"}"実行例の出力:
[2021-12-15 08:00:17] [INFO] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883**** {"key":"val"} FC Invoke End RequestId: 768945c8-f92d-428e-89c2-ecd50883**** Duration: 3.05 ms, Billed Duration: 4 ms, Memory Size: 256 MB, Max Memory Used: 10.77 MB ========= FC invoke Logs end ========= FC Invoke Result: OK End of method: invoke
参考資料
GPU 加速インスタンスを使用する場合は、カスタムコンテナ関数を作成する必要があります。GPU インスタンスの仕様選択についての詳細は、「インスタントタイプおよびモード」をご参照ください。
カスタムコンテナ関数は、コンテナイメージのベース環境をダウンロードおよび展開するのに追加の時間がかかるため、コールドスタート時間が長くなる可能性があります。コールドスタート時間を短縮するには、「コールドスタート時間の短縮に関するベストプラクティス」をご参照ください。
API を呼び出して関数を作成することもできます。「CreateFunction」をご参照ください。
Function Compute が提供する組み込みランタイム、カスタムランタイム、カスタムコンテナランタイムの適用範囲および違いについての詳細は、「関数ランタイムの選択」をご参照ください。