コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Cloud Functions for Firebase
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
Cloud Functions for Forebase はサーバーレス フレームワークです。バックグラウンド イベント、HTTPS リクエスト、
Admin SDK、
Cloud Scheduler ジョブによってトリガーされたイベントに応じて、バックエンド コードを自動的に実行できます。JavaScript、TypeScript、または Python のコードは Google Cloud インフラストラクチャに保存され、マネージド環境で実行されます。独自のサーバーを管理およびスケーリングする必要はありません。
Google Cloud で Cloud Functions をすでにご利用の場合は、Firebase がどのように適合するかについてご確認ください。
スタートガイド
ユースケース
主な機能
Firebase の機能を統合し、Firebase を Google Cloud に接続する |
関数を作成することで、Firebase Authentication トリガー、Cloud Storage トリガーなど、さまざまな Firebase や Google Cloud 機能によって生成されたイベントに応答できます。
Admin SDK を Cloud Functions とともに使用して、Firebase のさまざまな機能を統合できます。また、独自の Webhook を作成して、サードパーティのサービスと統合することもできます。Cloud Functions はボイラープレート コードを最小限に抑え、関数内で Firebase と Google Cloud を使いやすくします。 |
メンテナンス不要 |
コマンドラインから 1 つのコマンドで、JavaScript、TypeScript、または Python コードをサーバーにデプロイします。
その後は Firebase によって、ユーザーの使用パターンに合わせてコンピューティング リソースが自動的にスケールアップされます。認証情報、サーバー構成、新規サーバーのプロビジョニング、古いサーバーのデコミッションを気にする必要はありません。 |
ロジックの機密性と安全性を維持 |
多くの場合、デベロッパーはクライアント側での改ざんを防ぐために、サーバー上でアプリケーション ロジックを制御することを好みます。また、そのコードをリバース エンジニアリングできるようにすることは望ましくない場合もあります。Cloud Functions はクライアントから完全に隔離されているため、非公開であり、常に必要とするとおりに動作します。 |
仕組み
関数を記述してデプロイすると、Google のサーバーはすぐに関数の管理を開始します。関数は、HTTP リクエスト、Admin SDK、スケジュールされたジョブで直接呼び出すことができます。また、バックグラウンド関数の場合は、Google のサーバーがイベントをリッスンして、関数がトリガーされたときに実行します。
負荷が増減すると、Google は関数の実行に必要な仮想サーバー インスタンスの数を迅速にスケーリングすることで対応します。各関数は、独自の構成が含まれる独自の環境で独立して実行されます。
バックグラウンド関数のライフサイクル
- 新しい関数のコードを記述し、イベント プロバイダ(Cloud Firestore など)を選択し、関数の実行条件を定義します。
- 関数がデプロイされた時点:
- Firebase CLI によって関数コードの
.zip
アーカイブが作成され、Cloud Functions がプロジェクト内に Artifact Registry リポジトリ(gcf-artifacts
という名前)を作成する前に、Cloud Storage バケット(gcf-sources
の接頭辞付き)にアップロードされます。
- Cloud Build が関数コードを取得して、関数ソースをビルドします。Cloud Build ログは Google Cloud コンソールで確認できます。
- ビルドされた関数コードのコンテナ イメージがプロジェクト(
gcf-artifacts
という名前)内の非公開の Artifact Registry リポジトリにアップロードされ、新しい関数が展開されます。
- イベント プロバイダによって関数の条件に一致するイベントが生成されると、コードが呼び出されます。 関数には、Firebase Admin SDK を使用して他の Firebase サービスにアクセスする際に使用できるサービス アカウントが関連付けられています。
- 関数が多くのイベントの処理でビジー状態の場合、Google はより多くのインスタンスを作成して作業を迅速に処理します。関数がアイドル状態の場合、インスタンスはクリーンアップされます。
- 更新されたコードをデプロイすることで関数を更新すると、Artifact Registry のビルド アーティファクトとともに古いバージョンのインスタンスがクリーンアップされ、新しいインスタンスに置き換えられます。
- 関数を削除すると、すべてのインスタンスと zip アーカイブが、Artifact Registry の関連するビルド アーティファクトとともにクリーンアップされます。関数とイベント プロバイダ間の接続は削除されます。
バックグラウンド関数を使用するイベントをリッスンするだけでなく、HTTP リクエストまたはクライアントからの呼び出しで関数を直接呼び出すこともできます。 また、Admin SDK を使用して、一定のスケジュールで関数をトリガーしたり、タスク関数をキューに追加したりできます。
実装パス
|
Cloud Functionsを設定 |
Firebase CLI をインストールし、Firebase プロジェクトで Cloud Functions を初期化します。 |
|
関数の記述 |
Firebase サービス、Google Cloud サービス、他のイベント プロバイダからのイベントを処理する JavaScript コード、TypeScript コード、または Python コードを記述します。 |
|
関数のテスト |
関数をテストするにはローカル エミュレータを使用します。 |
|
デプロイとモニタリング |
プロジェクトを従量課金制の Blaze 料金プランにアップグレードし、Firebase CLI を使用して関数をデプロイします。Google Cloud コンソールを使用して、ログの表示や検索ができます。 |
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-17 UTC。
[null,null,["最終更新日 2025-08-17 UTC。"],[],[],null,["Cloud Functions for Firebase \nplat_ios plat_android plat_web plat_flutter plat_cpp plat_unity \nCloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by background events, HTTPS requests, the Admin SDK, or Cloud Scheduler jobs. Your JavaScript, TypeScript or Python code is stored on Google Cloud infrastructure and runs in a managed environment. There's no need to manage and scale your own servers.\n\n\u003cbr /\u003e\n\nAlready using Cloud Functions in Google Cloud?\n[Learn more](/docs/functions/functions-and-firebase) about how Firebase fits\ninto the picture.\n\n[Get started](/docs/functions/get-started)\n[Use cases](/docs/functions/use-cases)\n\nKey capabilities\n\n|----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Integrates Firebase features and connects Firebase with Google Cloud | The functions you write can respond to events generated by various Firebase and Google Cloud features, from [Firebase Authentication triggers](/docs/functions/auth-events) to [Cloud Storage Triggers](/docs/functions/gcp-storage-events). Integrate across Firebase features using the [Admin SDK](/docs/admin/setup) together with Cloud Functions, and integrate with third-party services by writing your own webhooks. Cloud Functions minimizes boilerplate code, making it easier to use Firebase and Google Cloud inside your function. |\n| Zero maintenance | Deploy your JavaScript, TypeScript, or Python code to our servers with one command from the command line. After that, Firebase automatically scales up computing resources to match the usage patterns of your users. You never worry about credentials, server configuration, provisioning new servers, or decommissioning old ones. |\n| Keeps your logic private and secure | In many cases, developers prefer to control application logic on the server to avoid tampering on the client side. Also, sometimes it's not desirable to allow that code to be reverse engineered. Cloud Functions is fully insulated from the client, so you can be sure it is private and always does exactly what you want. |\n\nHow does it work?\n\nAfter you write and deploy a function, Google's servers begin to manage the\nfunction immediately. You can fire the function directly with an HTTP request,\nthe Admin SDK, or a scheduled job, or, in the case of background functions,\nGoogle's servers listen for events and run the function when it is\ntriggered.\n\nAs the load increases or\ndecreases, Google responds by rapidly scaling the number of virtual server\ninstances needed to run your function. Each function runs in isolation, in its\nown environment with its own configuration.\n\nLifecycle of a background function\n\n1. You write code for a new function, selecting an event provider (such as Cloud Firestore), and defining the conditions under which the function should execute.\n2. When you deploy your function:\n 1. The Firebase CLI creates a `.zip` archive of the function code, which is then uploaded to a Cloud Storage bucket (prefixed with `gcf-sources`) before Cloud Functions creates an Artifact Registry repository (named `gcf-artifacts`) in your project.\n 2. Cloud Build retrieves the function code and builds the function source. You can view Cloud Build logs in the [Google Cloud console](//console.cloud.google.com/logs/query;query%3Dresource.type%253D%2522build%2522&sa=D&ust=1597179510979000&usg=AFQjCNG2BmFrgWjGoP83WiMrB2TLLXpBEQ).\n 3. The container image for the built functions code is uploaded to a private Artifact Registry repository in your project (named `gcf-artifacts`), and your new function is rolled out.\n3. When the event provider generates an event that matches the function's conditions, the code is invoked. The function has a service account attached to it that can be used to access other Firebase services with the help of the Firebase Admin SDK.\n4. If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.\n5. When you update the function by deploying updated code, instances for older versions are cleaned up along with build artifacts in Artifact Registry, and replaced by new instances.\n6. When you delete the function, all instances and zip archives are cleaned up, along with related build artifacts in Artifact Registry. The connection between the function and the event provider is removed.\n\nIn addition to listening for events with a background function, you can call\nfunctions directly with an HTTP request or a\n[call from the client](/docs/functions/callable). You can also trigger functions\non a fixed [schedule](/docs/functions/schedule-functions) or\n[enqueue task functions](/docs/functions/task-functions) via the Admin SDK.\n\nImplementation path\n\n|---|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Set up Cloud Functions | Install the Firebase CLI and initialize Cloud Functions in your Firebase project. |\n| | Write functions | Write JavaScript code, TypeScript code, or Python code to handle events from Firebase services, Google Cloud services, or other event providers. |\n| | Test functions | Use the [local emulator](/docs/functions/local-emulator) to test your functions. |\n| | Deploy and monitor | Upgrade your project to the [pay-as-you-go Blaze pricing plan](/pricing) and deploy your functions using the Firebase CLI. You can then use the [Google Cloud console](//console.cloud.google.com/functions/list) to view and search through your logs. |\n\nNext steps\n\n- [Get started](/docs/functions/get-started) setting up, creating, and deploying functions.\n- Learn more about [what you can do with functions](/docs/functions/use-cases).\n- Try the [Cloud Functions codelab](https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#0)."]]