透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Cloud Functions for Firebase
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
Cloud Functions for Firebase 是一種無伺服器架構,可讓您自動執行後端程式碼,以回應背景事件、HTTPS 要求、
Admin SDK 或
Cloud Scheduler 工作觸發的事件。您的 JavaScript、TypeScript 或 Python 程式碼會儲存在 Google Cloud 基礎架構中,並在代管環境中執行。您不必管理及擴充自己的伺服器。
已在 Google Cloud中使用 Cloud Functions?
進一步瞭解 Firebase 在這方面扮演的角色。
開始使用
用途
主要功能
整合 Firebase 功能,並將 Firebase 連接至 Google Cloud |
您編寫的函式可以回應各種 Firebase 和 Google Cloud 功能產生的事件,從 Firebase Authentication 觸發條件到 Cloud Storage 觸發條件。
使用 Admin SDK 和 Cloud Functions 整合 Firebase 功能,並編寫自己的 Webhook,與第三方服務整合。Cloud Functions 可減少樣板程式碼,讓您更輕鬆地在函式中使用 Firebase 和 Google Cloud。 |
免維護 |
在指令列中輸入指令,即可將 JavaScript、TypeScript 或 Python 程式碼部署至我們的伺服器。之後 Firebase 就會自動提高運算資源來因應使用者的使用模式,您再也不必擔心憑證、伺服器設定、新伺服器佈建或舊伺服器停用等問題。 |
確保邏輯私密且安全無虞 |
在多數情況下,開發人員偏好在伺服器上控制應用程式邏輯,以免不小心更動用戶端的任何設定。此外,有時也不宜允許反向工程該程式碼。Cloud Functions 與用戶端完全隔離,能提供完全私密的操作環境,確保一律只執行您想執行的作業。 |
運作原理
編寫及部署函式後,Google 伺服器會立即開始管理函式。您可以透過 HTTP 要求、Admin SDK或排定的工作直接觸發函式,也可以讓 Google 伺服器監聽事件,並在觸發事件時執行函式 (適用於背景函式)。
隨著負載增加或減少,Google 會快速調整執行函式所需的虛擬伺服器執行個體數量。每個函式都會在各自的環境中獨立執行,並有自己的設定。
背景函式的生命週期
- 您可以為新函式編寫程式碼、選取事件供應商 (例如 Cloud Firestore),並定義函式應執行的條件。
- 部署函式時:
- Firebase CLI 會建立函式程式碼的
.zip
封存檔,然後上傳至 Cloud Storage 值區 (前置字元為 gcf-sources
),接著 Cloud Functions 會在專案中建立 Artifact Registry 存放區 (名為 gcf-artifacts
)。
- Cloud Build 會擷取函式程式碼,並建構函式來源。您可以在 Google Cloud 控制台中查看 Cloud Build 記錄。
- 建構函式程式碼的容器映像檔會上傳至專案中的私有 Artifact Registry 存放區 (名為
gcf-artifacts
),並推出新函式。
- 當事件供應商產生符合函式條件的事件時,系統就會叫用程式碼。函式會附加服務帳戶,可用於透過 Firebase Admin SDK 存取其他 Firebase 服務。
- 如果函式忙於處理大量事件,Google 會建立更多執行個體,以便更快處理工作。如果函式處於閒置狀態,系統會清除執行個體。
- 部署更新後的程式碼來更新函式時,系統會清除舊版例項和 Artifact Registry 中的建構構件,並替換為新例項。
- 刪除函式時,系統會清除所有執行個體和 ZIP 封存檔,以及 Artifact Registry 中的相關建構構件。系統會移除函式與事件供應商之間的連結。
除了使用背景函式監聽事件,您也可以透過 HTTP 要求或從用戶端呼叫,直接呼叫函式。您也可以透過 Admin SDK,依固定排程觸發函式,或將工作函式加入佇列。
實作路徑
|
設定「Cloud Functions」 |
安裝 Firebase CLI,並在 Firebase 專案中初始化 Cloud Functions。 |
|
編寫函式 |
編寫 JavaScript、TypeScript 或 Python 程式碼,處理來自 Firebase 服務、Google Cloud 服務或其他事件供應商的事件。 |
|
測試函式 |
使用本機模擬器測試函式。 |
|
部署及監控 |
將專案升級至即付即用 Blaze 定價方案,然後使用 Firebase CLI 部署函式。然後使用
Google Cloud 控制台查看及搜尋記錄。 |
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-17 (世界標準時間)。
[null,null,["上次更新時間:2025-08-17 (世界標準時間)。"],[],[],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)."]]