Google Cloud CLI を使用して Cloud Functions(第 2 世代)の関数を作成してデプロイする
このページでは、Google Cloud CLI を使用して、第 2 世代の Cloud Functions の関数を作成してデプロイする方法について説明します。
始める前に
- Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud プロジェクトに対して課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。
-
Cloud Functions and Cloud Build API を有効にします。
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud プロジェクトに対して課金が有効になっていることを確認します。詳しくは、プロジェクトで課金が有効になっているかどうかを確認する方法をご覧ください。
-
Cloud Functions and Cloud Build API を有効にします。
- gcloud CLI をインストールして初期化します。
gcloud
コンポーネントを更新します。gcloud components update
- 開発環境を準備します。
Node.js
Python
Go
Java
C#
Ruby
PHP
コマンド プロンプトが必要な場合は、Google Cloud Shell を使用できます。Google Cloud Shell は、Google Cloud CLI がすでに含まれているコマンドライン環境であるため、インストールする必要はありません(Google Cloud CLI は、Google Compute Engine の仮想マシンにもプリインストールされています)。
サンプルコードを取得する
ローカルマシンにサンプル リポジトリのクローンを作成します。
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/functionsv2/helloworld/
Python
cd python-docs-samples/functions/functionsv2/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=nodejs16 \ --region=優先する Node.js のバージョンを指定するには、REGION
\ --source=. \ --entry-point=helloGET \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。
nodejs16
(推奨)nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy python-http-function \ --gen2 \ --runtime=python310 \ --region=優先する Python バージョンを指定するには、REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。
python310
(推奨)python39
python38
python37
Go
gcloud functions deploy go-http-function \ --gen2 \ --runtime=go116 \ --region=優先する Go バージョンを指定するには、REGION
\ --source=. \ --entry-point=HelloGet \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。
go116
(推奨)go113
go111
Java
gcloud functions deploy java-http-function \ --gen2 \ --runtime=java17 \ --region=優先する Java バージョンを指定するには、REGION
\ --source=. \ --entry-point=functions.HelloWorld \ --memory=512MB \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。
java17
(推奨)java11
C#
gcloud functions deploy csharp-http-function \ --gen2 \ --runtime=dotnet3 \ --region=REGION
\ --source=. \ --entry-point=HelloWorld.Function \
--trigger-http \
--allow-unauthenticated
Ruby
gcloud functions deploy ruby-http-function \ --gen2 \ --runtime=ruby30 \ --region=優先する Ruby バージョンを指定するには、REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。ruby30
(推奨)ruby27
ruby26
PHP
gcloud functions deploy php-http-function \ --gen2 \ --runtime=php81 \ --region=優先する PHP バージョンを指定するには、REGION
\ --source=. \ --entry-point=helloGet \
--trigger-http \
--allow-unauthenticated
--runtime
フラグに次の値を使用します。
php81
(推奨)php74
--allow-unauthenticated
フラグを使用すると、認証なしで関数にアクセスできます。認証を要求するには、フラグを省略します。
関数のトリガー
関数がデプロイされたら、
uri
プロパティをメモするか、次のコマンドを使用して検索します。Node.js
gcloud functions describe nodejs-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
Python
gcloud functions describe python-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
Go
gcloud functions describe go-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
Java
gcloud functions describe java-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
C#
gcloud functions describe csharp-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
Ruby
gcloud functions describe ruby-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
PHP
gcloud functions describe php-http-function --gen2 --region REGION --format="value(serviceConfig.uri)"
ブラウザでこの URI にアクセスします。
Hello World!
というメッセージが表示されます。
Cloud Functions の関数の削除
このチュートリアルで作成した 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 を削除することもできます。