Firebase Cloud Functions
This guide shows you how to work with Google Cloud Firebase Functions locally using the Firebase Emulator Suite. This lab demonstrates a synchronous HTTPS function that retrieves a secret from Secret Manager based on user name.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in ContainerRun the setup script to install tools and dependencies:
Terminal window scripts/setup.sh -
Start Firebase Emulators:
Terminal window firebase emulators:start -
Run the Example:
Terminal window python main.pyTerminal window curl "http://localhost:5001/demo-mve-firebase-functions/us-central1/get_secret?username=admin"- Install: REST Client from de VS Code Marketplace.
- Open: Open
http/get_secret.httpin VS Code. - Run: Click Send Request above any request block.
How to debug
Section titled “How to debug”-
Cloud Function:
- Ensure: Stop the Firebase Emulator before attaching the debugger (it conflicts with the debugger port).
- Run: Launch the Debug Cloud Function (Local) configuration from the Run and Debug tab.
- Breakpoints: Set breakpoints inside
get_secretinfunctions/main.py. - Send: Trigger a request via curl or the REST Client pointing to
http://localhost:8080/.
-
main.py:
- Ensure: Stop the Firebase Emulator before attaching the debugger.
- Run: Launch the Debug Cloud Function (Local) configuration.
- Set: Set
DEBUG_MODE=Truein your.env. - Run: Open
main.pyand start the Python: Current File debugger.
-
Tests:
- Ensure: The Firebase Emulator must be running for individual test debugging.
- Breakpoints: Set breakpoints in
tests/test_get_secret.py. - Run: Open the Testing tab and click the Debug Test icon next to the desired test.
How to test
Section titled “How to test”- All tests: Run the automated script:
Terminal window scripts/run_tests.sh - Individually: Use the VS Code Testing tab to run or debug specific test cases.
Validate results
Section titled “Validate results”Open the Firebase Emulator UI and navigate to the Logs section to inspect function activity:
http://localhost:4000Clean Up
Section titled “Clean Up”Stop the emulators by pressing Ctrl+C in the terminal running firebase emulators:start. To remove volumes:
docker compose down -v