このシンプルなチュートリアルでは、HTTP Cloud Functions の関数で書き込み、デプロイ、トリガーを行う方法について説明します。
目標
- HTTP Cloud Functions の関数を書き込み、デプロイ、トリガーする。
料金
このドキュメントでは、Google Cloud の次の課金対象のコンポーネントを使用します。
- Cloud Functions
- Cloud Build
- Artifact Registry
詳細については、Cloud Functions の料金をご覧ください。
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 開発環境を準備します。
Node.js
Python
Go
Java
C#
Ruby
PHP
gcloud CLI がすでにインストールされている場合は、次のコマンドを実行して更新します。
gcloud components update
アプリケーションの準備
ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
また、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
また、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
また、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
また、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
また、zip 形式のサンプルをダウンロードしてファイルを抽出してもかまいません。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
また、zip 形式のサンプルをダウンロードし、ファイルを抽出してもかまいません。
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
または、zip 形式のサンプルをダウンロードし、ファイルを抽出してもかまいません。
Cloud Functions のサンプルコードが含まれているディレクトリに移動します。
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/functionsv2/helloworld/
Java
cd java-docs-samples/functions/helloworld/helloworld/
C#
cd dotnet-docs-samples/functions/helloworld/HelloWorld/
Ruby
cd ruby-docs-samples/functions/helloworld/get/
PHP
cd php-docs-samples/functions/helloworld_get/
サンプルコードを見てみましょう。
Node.js
Python
Go
Java
C#
Ruby
PHP
関数のデプロイ
HTTP トリガーを使用して関数をデプロイするには、サンプルコード(Java の場合は pom.xml
ファイル)を含むディレクトリで次のコマンドを実行します。
Node.js
gcloud functions deploy nodejs-http-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGET \
--trigger-http
サポートされている Node.js バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Python
gcloud functions deploy python-http-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http
サポートされている Python バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Go
gcloud functions deploy go-http-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloGet \
--trigger-http
サポートされている Go バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Java
gcloud functions deploy java-http-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloWorld \ --memory=512MB \
--trigger-http
サポートされている Java バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
C#
gcloud functions deploy csharp-http-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloWorld.Function \
--trigger-http
サポートされている .NET バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
Ruby
gcloud functions deploy ruby-http-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http
サポートされている Ruby バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
PHP
gcloud functions deploy php-http-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGet \
--trigger-http
サポートされている PHP バージョンのランタイム ID を指定して関数を実行するには、--runtime
フラグを使用します。
必要なら --allow-unauthenticated
フラグを使用し、認証なしで関数にアクセスできます。これはテストに便利ですが、公開 API やウェブサイトを作成する場合を除き、本番環境でこの設定を使用することはおすすめしません。また、会社のポリシーの設定によっては、これは機能しない可能性があります。認証が必要な関数を呼び出す方法については、呼び出しの認証をご覧ください。
関数のトリガー
ブラウザで、この URL にアクセスします。
Hello, World!
というメッセージが表示されます。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
プロジェクトの削除
課金をなくす最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。
プロジェクトを削除するには:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Cloud Functions の関数の削除
Cloud Functions を削除しても、Cloud Storage に保存されたリソースは削除されません。
このチュートリアルで作成した Cloud Functions の関数を削除するには、次のコマンドを実行します。
Node.js
gcloud functions delete nodejs-http-function --gen2 --region REGION
Python
gcloud functions delete python-http-function --gen2 --region REGION
Go
gcloud functions delete go-http-function --gen2 --region REGION
Java
gcloud functions delete java-http-function --gen2 --region REGION
C#
gcloud functions delete csharp-http-function --gen2 --region REGION
Ruby
gcloud functions delete ruby-http-function --gen2 --region REGION
PHP
gcloud functions delete php-http-function --gen2 --region REGION
Google Cloud Console から Cloud Functions を削除することもできます。