จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Cloud Functions สำหรับ Firebase
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
Cloud Functions สำหรับ Firebase เป็นเฟรมเวิร์กแบบ Serverless ที่
ช่วยให้คุณเรียกใช้โค้ดแบ็กเอนด์ได้โดยอัตโนมัติเพื่อตอบสนองต่อเหตุการณ์ที่ทริกเกอร์โดย
เหตุการณ์ในเบื้องหลัง คำขอ HTTPS,
Admin SDK หรือ งาน
Cloud Scheduler ระบบจะจัดเก็บโค้ด JavaScript, TypeScript หรือ Python ของคุณไว้ในโครงสร้างพื้นฐานของ Google Cloud และเรียกใช้ในสภาพแวดล้อมที่มีการจัดการ คุณไม่จำเป็นต้องจัดการและปรับขนาดเซิร์ฟเวอร์ของคุณเอง
หากใช้ Cloud Functions ใน Google Cloud อยู่แล้ว
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีที่ Firebase
เข้ามามีบทบาท
เริ่มต้นใช้งาน
กรณีการใช้งาน
ความสามารถหลัก
ผสานรวมฟีเจอร์ของ Firebase และเชื่อมต่อ Firebase กับ
Google Cloud |
ฟังก์ชันที่คุณเขียนสามารถตอบสนองต่อเหตุการณ์ที่สร้างขึ้นโดยฟีเจอร์ต่างๆ ของ Firebase และ Google Cloud ได้ ตั้งแต่ทริกเกอร์การตรวจสอบสิทธิ์ Firebase
ไปจนถึงทริกเกอร์ Cloud Storage
ผสานรวมฟีเจอร์ต่างๆ ของ Firebase โดยใช้
Admin SDK
ร่วมกับ Cloud Functions และผสานรวมกับบริการของบุคคลที่สาม
โดยการเขียน 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 จะดึงโค้ดฟังก์ชันและสร้างแหล่งที่มาของฟังก์ชัน
คุณดูCloud Buildบันทึกได้ในคอนโซล Google Cloud
- ระบบจะอัปโหลดอิมเมจคอนเทนเนอร์สำหรับโค้ดฟังก์ชันที่สร้างไปยังArtifact Registryที่เก็บส่วนตัวในโปรเจ็กต์
(ชื่อ
gcf-artifacts
) และจะเปิดตัวฟังก์ชันใหม่
- เมื่อผู้ให้บริการเหตุการณ์สร้างเหตุการณ์ที่ตรงกับเงื่อนไขของฟังก์ชัน ระบบจะเรียกใช้โค้ด ฟังก์ชันมีบัญชีบริการที่แนบมา
ซึ่งใช้เพื่อเข้าถึงบริการอื่นๆ ของ Firebase ได้โดยใช้Firebase Admin SDK
- หากฟังก์ชันกำลังจัดการเหตุการณ์จำนวนมาก Google จะสร้างอินสแตนซ์เพิ่มเติม
เพื่อจัดการงานได้เร็วขึ้น หากฟังก์ชันไม่ได้ใช้งาน ระบบจะล้างข้อมูลอินสแตนซ์
- เมื่ออัปเดตฟังก์ชันโดยการติดตั้งใช้งานโค้ดที่อัปเดตแล้ว ระบบจะล้างอินสแตนซ์สำหรับเวอร์ชันเก่ากว่า
พร้อมกับอาร์ติแฟกต์การสร้างใน
Artifact Registry และแทนที่ด้วยอินสแตนซ์ใหม่
- เมื่อลบฟังก์ชัน ระบบจะล้างข้อมูลอินสแตนซ์และไฟล์เก็บถาวร ZIP ทั้งหมด
รวมถึงอาร์ติแฟกต์การบิลด์ที่เกี่ยวข้องใน Artifact Registry
ระบบจะนำการเชื่อมต่อระหว่างฟังก์ชันและผู้ให้บริการเหตุการณ์ออก
นอกเหนือจากการรอฟังเหตุการณ์ด้วยฟังก์ชันพื้นหลังแล้ว คุณยังเรียกใช้ฟังก์ชันได้โดยตรงด้วยคำขอ HTTP หรือการเรียกจากไคลเอ็นต์ นอกจากนี้ คุณยังทริกเกอร์ฟังก์ชัน
ตามกำหนดการที่แน่นอนหรือ
ฟังก์ชันงานในคิวผ่าน Admin SDK ได้ด้วย
เส้นทางการติดตั้งใช้งาน
|
ตั้งค่า Cloud Functions |
ติดตั้ง Firebase CLI และเริ่มต้น
Cloud Functions ในโปรเจ็กต์ Firebase |
|
เขียนฟังก์ชัน |
เขียนโค้ด JavaScript, โค้ด TypeScript หรือโค้ด Python
เพื่อจัดการเหตุการณ์จากบริการ Firebase
Google Cloud หรือผู้ให้บริการเหตุการณ์อื่นๆ |
|
ฟังก์ชันทดสอบ |
ใช้โปรแกรมจำลองในเครื่อง
เพื่อทดสอบฟังก์ชัน |
|
ติดตั้งใช้งานและตรวจสอบ |
อัปเกรดโปรเจ็กต์เป็นแพ็กเกจราคา 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)."]]