クイックスタート: gcloud CLI で Cloud Run functions の関数をデプロイする (original) (raw)
このページでは、gcloud CLI を使用して Cloud Run functions の 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.
Install the Google Cloud CLI.
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud initCreate or select 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. - Create a Google Cloud project:
gcloud projects create PROJECT_ID
ReplacePROJECT_IDwith a name for the Google Cloud project you are creating. - Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
ReplacePROJECT_IDwith your Google Cloud project name.
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging 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 services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.comGrant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
Install the Google Cloud CLI.
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
gcloud CLI を初期化するには、次のコマンドを実行します。
gcloud initCreate or select 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. - Create a Google Cloud project:
gcloud projects create PROJECT_ID
ReplacePROJECT_IDwith a name for the Google Cloud project you are creating. - Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
ReplacePROJECT_IDwith your Google Cloud project name.
- Verify that billing is enabled for your Google Cloud project.
- Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging 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 services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com - Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
- 次のコマンドを実行して、Cloud Run サービスのデフォルト プロジェクトを設定します。
gcloud config set project PROJECT_ID
PROJECT_ID は、このクイックスタートで作成したプロジェクトの名前に置き換えます。 - ドメイン制限の組織のポリシーでプロジェクトの未認証呼び出しが制限されている場合は、限定公開サービスのテストの説明に従って、デプロイされたサービスにアクセスする必要があります。
- サービス ID にサービス アカウント ユーザーのロールが付与されていることを確認します。デフォルトでは、サービス ID は Compute Engine のデフォルトのサービス アカウントです。
ロールを付与する
サービス ID リソースに対するアクセス権を付与するには、gcloud iam service-accounts add-iam-policy-binding コマンドを使用します。ハイライト表示された変数は、適切な値に置き換えてください。
gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
--member=user:PRINCIPAL \
--role=roles/iam.serviceAccountUser
次のように置き換えます。
- SERVICE_ACCOUNT_EMAIL: サービス ID として使用しているサービス アカウントのメールアドレス。たとえば次のようなものです。
* Compute Engine のデフォルトのサービス アカウント:PROJECT_NUMBER-compute@developer.gserviceaccount.com
* 作成したサービス アカウント:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com - PRINCIPAL: ユーザー ID。これは通常、Cloud Run リソースをデプロイするメールアドレスです。
- Cloud Build サービス アカウントに次の IAM ロールを付与します。
ここをクリックして Cloud Build サービス アカウントに必要なロールを表示
この動作をオーバーライドしない限り、Cloud Build は、ソースコードと Cloud Run リソースのビルドにデフォルトの Cloud Build サービス アカウントとして Compute Engine のデフォルトのサービス アカウントを自動的に使用します。Cloud Build がソースをビルドできるようにするには、プロジェクトの Compute Engine のデフォルトのサービス アカウントに Cloud Run ビルダー(roles/run.builder)を付与するよう管理者に依頼します。
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
--role=roles/run.builder
PROJECT_NUMBER は Google Cloudプロジェクト番号に、PROJECT_ID は Google Cloudプロジェクト ID に置き換えます。プロジェクト ID とプロジェクト番号を確認する方法については、プロジェクトの作成と管理をご覧ください。
Compute Engine のデフォルト サービス アカウントに Cloud Run ビルダーのロールを付与すると、反映されるまでに数分かかることがあります。
20. Cloud Run の料金を確認するか、料金計算ツールで費用を見積もります。
サンプル関数を作成する
アプリケーションを作成するには、次の操作を行います。
Node.js
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld helloworldディレクトリにpackage.jsonファイルを作成して、Node.js の依存関係を指定します。- 次の Node.js サンプルを使用して、
helloworldディレクトリにindex.jsファイルを作成します。
Python
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld - Python の依存関係を指定するために、
helloworldディレクトリにrequirements.txtファイルを作成します。
これにより、サンプルで必要なパッケージが追加されます。 helloworldディレクトリに、次の Python サンプルを含むmain.pyファイルを作成します。
Go
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld go.modファイルを作成して go モジュールを宣言します。helloworldディレクトリに、次の Go コードサンプルを含むhello_http.goファイルを作成します。
Java
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld - ソース ディレクトリとソースファイルを格納する次のプロジェクト構造を作成します。
mkdir -p ~/helloworld/src/main/java/functions
touch ~/helloworld/src/main/java/functions/HelloWorld.java - 次の Java コードサンプルで
HelloWorld.javaファイルを更新します。 helloworldディレクトリにpom.xmlファイルを作成し、次の Java 依存関係を追加します。
Ruby
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld app.rbという名前のファイルを作成し、そのファイルに次のコードを貼り付けます。Gemfileという名前のファイルを作成し、そのファイルに次の内容をコピーします。- Bundler 2.0 以降がインストールされていない場合は、Bundler をインストールします。
- 次のコマンドを実行して
Gemfile.lockファイルを生成します。
bundle install PHP
helloworldという名前の新しいディレクトリを作成し、そのディレクトリに移動します。
mkdir helloworld
cd helloworld index.phpという名前のファイルを作成し、そのファイルに次のコードを貼り付けます。- Cloud Shell を使用していない場合は、
composer.jsonファイルを作成し、そのファイルに次のコードを貼り付けます。
.NET
- .NET SDK をインストールします。
- コンソールから dotnet コマンドを使用して、新しい空のウェブ プロジェクトを作成します。
dotnet new web -o helloworld-csharp helloworld-csharpディレクトリに移動します。- プロジェクト ファイル
helloworld-csharp.csprojのサンプルコードを次のコードに置き換えます。 Program.csファイルのサンプルコードを次のコードに置き換えます。
関数をデプロイする
重要: 以下の説明では、このクイックスタートで使用するプロジェクトのオーナーロールまたは編集者ロールが付与されていることを前提としています。ロールが付与されていない場合は、Cloud Run ソース デベロッパー ロールで、ソースから Cloud Run リソースをデプロイするために必要な権限を確認してください。
Cloud Run 関数をデプロイするには、次の操作を行います。
- サンプルコードを含むディレクトリで次のコマンドを実行して、関数をデプロイします。
Node.js
gcloud run deploy nodejs-http-function \
--source . \
--function helloGET \
--base-image nodejs22 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
Python
gcloud run deploy python-http-function \
--source . \
--function hello_get \
--base-image python313 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
Go
gcloud run deploy go-http-function \
--source . \
--function HelloGet \
--base-image go125 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
Java
pom.xml ファイルが含まれるディレクトリで、次のコマンドを実行します。
gcloud run deploy java-http-function \
--source . \
--function functions.HelloWorld \
--base-image java21 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
Ruby
gcloud run deploy ruby-http-function \
--source . \
--function hello_get \
--base-image ruby34 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
PHP
gcloud run deploy php-http-function \
--source . \
--function helloGet \
--base-image php84 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
.NET
gcloud run deploy csharp-http-function \
--source . \
--function HelloWorld.Function \
--base-image dotnet8 \
--region REGION \
--allow-unauthenticated REGION は、関数をデプロイするサービスの Google Cloudリージョンに置き換えます。例: europe-west1
2. デプロイが完了すると、サービスが実行されている URL が Google Cloud CLI に表示されます。ブラウザで URL を開き、関数の出力を確認します。
クリーンアップ
Google Cloud アカウントで追加料金が発生しないようにするには、このクイックスタートでデプロイしたすべてのリソースを削除します。
リポジトリを削除する
デプロイされたサービスが使用されていない場合、Cloud Run の料金は発生しません。ただし、コンテナ イメージを Artifact Registry に保存した場合にも料金が発生する可能性があります。Artifact Registry リポジトリを削除するには、Artifact Registry ドキュメントのリポジトリを削除するの手順を行います。
サービスを削除する
Cloud Run サービスの費用は、リクエストを受け取るまでは発生しません。Cloud Run サービスを削除するには、次のいずれかの操作を行います。
コンソール
サービスを削除するには:
- Google Cloud コンソールで、Cloud Run の [サービス] ページに移動します。
Cloud Run に移動 - 削除するサービスをサービスリストで探し、そのチェックボックスをクリックして選択します。
- [削除] をクリックします。これにより、サービスのすべてのリビジョンが削除されます。
gcloud
サービスを削除するには、次のコマンドを実行します。
gcloud run services delete SERVICE --region REGION
次のように置き換えます。
- SERVICE: サービスの名前。
- REGION: サービスの Google Cloud リージョン。
テスト プロジェクトを削除する
Google Cloud プロジェクトを削除すると、そのプロジェクト内のすべてのリソースに対する課金が停止します。プロジェクト内のすべての Google Cloud リソースを解放する手順は次のとおりです。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
次のステップ
- Google Cloud コンソールを使用してサンプル関数を Cloud Run にデプロイする。クイックスタート: Google Cloud コンソールを使用して関数を Cloud Run にデプロイするをご覧ください。
- Google Cloud コンソールと Google Cloud CLI を使用して関数をデプロイし、トリガーを作成する。関数をデプロイするをご覧ください。
- 既存の関数を表示して削除する。サービスのリビジョンを管理するをご覧ください。
- 独自のツールチェーンで関数コンテナをビルドして Cloud Run にデプロイする。関数をビルドするをご覧ください。
- Eventarc でトリガーを作成する。Eventarc でトリガーを作成するをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-11-12 UTC。