クイックスタート: gcloud CLI で Cloud Run functions の関数をデプロイする (original) (raw)

このページでは、gcloud CLI を使用して Cloud Run functions の HTTP 関数をデプロイする方法について説明します。

始める前に

  1. 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.

  2. Install the Google Cloud CLI.

  3. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  4. gcloud CLI を初期化するには、次のコマンドを実行します。
    gcloud init

  5. Create 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.create permission. Learn how to grant roles.
    • Create a Google Cloud project:
      gcloud projects create PROJECT_ID
      Replace PROJECT_ID with a name for the Google Cloud project you are creating.
    • Select the Google Cloud project that you created:
      gcloud config set project PROJECT_ID
      Replace PROJECT_ID with your Google Cloud project name.
  6. Verify that billing is enabled for your Google Cloud project.

  7. 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.enable permission. Learn how to grant roles.
    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com

  8. 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.
  9. Install the Google Cloud CLI.

  10. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  11. gcloud CLI を初期化するには、次のコマンドを実行します。
    gcloud init

  12. Create or select a Google Cloud project.
    Roles required to select or create a project

  1. Verify that billing is enabled for your Google Cloud project.
  2. 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.enable permission. Learn how to grant roles.
    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com
  3. 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:
  1. 次のコマンドを実行して、Cloud Run サービスのデフォルト プロジェクトを設定します。
    gcloud config set project PROJECT_ID
    PROJECT_ID は、このクイックスタートで作成したプロジェクトの名前に置き換えます。
  2. ドメイン制限の組織のポリシーでプロジェクトの未認証呼び出しが制限されている場合は、限定公開サービスのテストの説明に従って、デプロイされたサービスにアクセスする必要があります。
  3. サービス 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

次のように置き換えます。

  1. 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

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. helloworld ディレクトリに package.json ファイルを作成して、Node.js の依存関係を指定します。
  2. 次の Node.js サンプルを使用して、helloworld ディレクトリに index.js ファイルを作成します。

Python

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. Python の依存関係を指定するために、helloworld ディレクトリに requirements.txt ファイルを作成します。
    これにより、サンプルで必要なパッケージが追加されます。
  2. helloworld ディレクトリに、次の Python サンプルを含む main.py ファイルを作成します。

Go

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. go.mod ファイルを作成して go モジュールを宣言します。
  2. helloworld ディレクトリに、次の Go コードサンプルを含む hello_http.go ファイルを作成します。

Java

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. ソース ディレクトリとソースファイルを格納する次のプロジェクト構造を作成します。
mkdir -p ~/helloworld/src/main/java/functions  
touch ~/helloworld/src/main/java/functions/HelloWorld.java  
  1. 次の Java コードサンプルで HelloWorld.java ファイルを更新します。
  2. helloworld ディレクトリに pom.xml ファイルを作成し、次の Java 依存関係を追加します。

Ruby

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. app.rb という名前のファイルを作成し、そのファイルに次のコードを貼り付けます。
  2. Gemfile という名前のファイルを作成し、そのファイルに次の内容をコピーします。
  3. Bundler 2.0 以降がインストールされていない場合は、Bundler をインストールします。
  4. 次のコマンドを実行して Gemfile.lock ファイルを生成します。
bundle install  

PHP

  1. helloworld という名前の新しいディレクトリを作成し、そのディレクトリに移動します。
   mkdir helloworld  
   cd helloworld  
  1. index.php という名前のファイルを作成し、そのファイルに次のコードを貼り付けます。
  2. Cloud Shell を使用していない場合は、composer.json ファイルを作成し、そのファイルに次のコードを貼り付けます。

.NET

  1. .NET SDK をインストールします。
  2. コンソールから dotnet コマンドを使用して、新しい空のウェブ プロジェクトを作成します。
dotnet new web -o helloworld-csharp  
  1. helloworld-csharp ディレクトリに移動します。
  2. プロジェクト ファイル helloworld-csharp.csproj のサンプルコードを次のコードに置き換えます。
  3. Program.cs ファイルのサンプルコードを次のコードに置き換えます。

関数をデプロイする

重要: 以下の説明では、このクイックスタートで使用するプロジェクトのオーナーロールまたは編集者ロールが付与されていることを前提としています。ロールが付与されていない場合は、Cloud Run ソース デベロッパー ロールで、ソースから Cloud Run リソースをデプロイするために必要な権限を確認してください。

Cloud Run 関数をデプロイするには、次の操作を行います。

  1. サンプルコードを含むディレクトリで次のコマンドを実行して、関数をデプロイします。

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 サービスを削除するには、次のいずれかの操作を行います。

コンソール

サービスを削除するには:

  1. Google Cloud コンソールで、Cloud Run の [サービス] ページに移動します。
    Cloud Run に移動
  2. 削除するサービスをサービスリストで探し、そのチェックボックスをクリックして選択します。
  3. [削除] をクリックします。これにより、サービスのすべてのリビジョンが削除されます。

gcloud

サービスを削除するには、次のコマンドを実行します。

gcloud run services delete SERVICE --region REGION

次のように置き換えます。

テスト プロジェクトを削除する

Google Cloud プロジェクトを削除すると、そのプロジェクト内のすべてのリソースに対する課金が停止します。プロジェクト内のすべての Google Cloud リソースを解放する手順は次のとおりです。

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

次のステップ

特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。

最終更新日 2025-11-12 UTC。