Python で Cloud Run functions の HTTP 関数を作成してデプロイする(第 1 世代)
このガイドでは、Python ランタイムを使用して Cloud Run functions の関数を記述するプロセスを説明します。Cloud Run functions には次の 2 つのタイプがあります。
- HTTP 関数。標準的な HTTP リクエストから呼び出します。
- イベント ドリブン関数。Pub/Sub トピックのメッセージや Cloud Storage バケットの変更など、Cloud インフラストラクチャのイベントを処理するために使用します。
次のサンプルでは、簡単な HTTP 関数を作成しています。
始める前に
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - gcloud CLI をインストールして初期化します。
gcloudコンポーネントを更新してインストールします。gcloud components update
- 開発環境を準備します。
関数コードで使用するため、ローカル システムにディレクトリを作成します。
Linux / Mac OS X
mkdir ~/helloworld cd ~/helloworldWindows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworldhelloworldディレクトリに、次の内容のmain.pyファイルを作成します。このサンプル関数は HTTP リクエストで指定された名前を使用します。名前が指定されていない場合は、「Hello World!」という挨拶を返します。
helloworldディレクトリにrequirements.txtファイルを作成します。関数の依存関係を
requirements.txtファイルに追加します。次に例を示します。# An example requirements file, add your dependencies below sampleproject==2.0.0関数がデプロイされたら、
httpsTrigger.urlプロパティをメモするか、次のコマンドを使用して検索します。gcloud functions describe hello_http次のようになります。
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
ブラウザで、この URL にアクセスします。「Hello World!」というメッセージが表示されます。
たとえば、次の URL を使用して HTTP リクエストで名前を渡します。
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME
「Hello
NAME!」というメッセージが表示されます。
関数を作成する
依存関係を指定する
Python における依存関係は pip で管理され、requirements.txt というメタデータ ファイルで表現されます。このファイルは、関数のコードを含む main.py ファイルと同じディレクトリに置く必要があります。
この特定のサンプルを実行するために requirements.txt を作成する必要はありませんが、独自の依存関係を追加するとします。方法は次のとおりです。
関数をデプロイする
HTTP トリガーを使用して関数をデプロイするには、helloworld ディレクトリで次のコマンドを実行します。
gcloud functions deploy hello_http --no-gen2 --runtime python313 --trigger-http --allow-unauthenticated
--allow-unauthenticated フラグを使用すると、認証なしで関数にアクセスできます。認証を要求するには、フラグを省略します。
関数をテストする
ログを表示する
Cloud Run functions のログは、Google Cloud CLI を使用して、また Cloud Logging UI で表示できます。
コマンドライン ツールを使用する
gcloud CLI を使用して関数のログを表示するには、logs read コマンドの後に関数の名前を続けます。
gcloud functions logs read hello_http
出力は次のようになります。
LEVEL NAME EXECUTION_ID TIME_UTC LOG D hello_http pdb5ys2t022n 2019-09-18 23:29:09.791 Function execution started D hello_http pdb5ys2t022n 2019-09-18 23:29:09.798 Function execution took 7 ms, finished with status code: 200
Logging ダッシュボードを使用する
Google Cloud コンソールで Cloud Run functions のログを確認することもできます。