Skip to content

Cloud Function deploy fails for Firestore-triggered function #6364

Closed

Description

@dominoFire

[REQUIRED] Environment info

firebase-tools: 12.4.7

Platform: Windows

[REQUIRED] Test case

When I try to deploy a triggered Python, 2nd-gen cloud function, it fails with a database not found error.

[REQUIRED] Steps to reproduce

  1. Create a function in your main.py:
@firestore_fn.on_document_created(document='a/{aId}/b/{bId}')
def do_magic(event: Event[DocumentSnapshot]) -> None:
    """
    Do magic
    """


    return None
  1. Run firebase deploy on the function.

[REQUIRED] Expected behavior

Deploys succeeds.

[REQUIRED] Actual behavior

Deploy fails with the following message:

firebase-debug.log


=== Deploying to 'propio-app'...

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
i  functions: Loading and analyzing source code for codebase default to determine what to deploy
 * Serving Flask app 'serving'
 * Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8081
Press CTRL+C to quit

127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/functions.yaml HTTP/1.1" 200 -

2023-09-15 13:13:27,719 - werkzeug - INFO - 127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/quitquitquit HTTP/1.1" 200 -

2023-09-15 13:13:27,728 - werkzeug - INFO - 127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/quitquitquit HTTP/1.1" 200 -

i  functions: preparing functions directory for uploading...
i  functions: packaged C:\src\propio3\webapp\backend\functions (40.74 KB) for uploading
There was an error retrieving the Firestore database. Currently, the database id is set to (default), make sure it exists.

Error: Failed to make request to http://127.0.0.1:8080/v1/projects/propio-app/databases/(default)

Activity

  1. dominoFire commented on Sep 15, 2023

    @dominoFire
    Author

    Please help me diagnosing this issue. I'm blocked on writing trigger cloud functions and I don't want to default to HTTPs functions.

    This issue also happens in firebase-tools 12.5.4

  2. changed the title [-]Cloud Function deploy fails [/-] [+]Cloud Function deploy fails for Firestore-triggered function[/+] on Sep 15, 2023
  3. aalej commented on Sep 19, 2023

    @aalej
    Contributor

    Hi @dominoFire, thanks for reaching out and for providing a detailed report. I tried to reproduce the behavior you mentioned, but I encountered no errors when trying to deploy the code snippet you provided.

    One thing I noticed is that in your debug logs, it makes a request to GET http://127.0.0.1:8080/v1/projects/<project_id>/databases/(default). I tried comparing this to my debug logs, and mine shows GET https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default).

    Could you provide additional information such as your firebase.json file or if you’re setting any environment variables during/before deploying? Please remove any PII (personal identifiable information) before sharing the file.

  4. added
    Needs: Author Feedback
    on Sep 19, 2023
  5. joehan commented on Sep 19, 2023

    @joehan
    Member

    Specifically, could you check that the FIRESTORE_EMULATOR_HOST and FIRESTORE_URL environment vairables are not set? Looking through the provided debug logs, it seems like one of them must be set & that is causing your requests to go to the wrong host.

  6. jjgriff93 commented on Sep 25, 2023

    @jjgriff93

    Getting this now as well after upgrading from a Firestore v1 trigger to v2

  7. google-oss-bot commented on Oct 4, 2023

    @google-oss-bot
    Collaborator

    Hey @dominoFire. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

    If you have more information that will help us get to the bottom of this, just add a comment!

  8. landa commented on Oct 4, 2023

    @landa

    I was getting the same error and it was fixed after unsetting @joehan's environment variables:

    vscode ➜ /workspaces/functions (main) $ export FIRESTORE_EMULATOR_HOST=
    vscode ➜ /workspaces/functions (main) $ firebase deploy --only functions:handle_dataset_created
    

    It seems that this is a bug in the Firebase CLI as using the emulators shouldn't interfere with deploying.

  9. dominoFire commented on Oct 6, 2023

    @dominoFire
    Author

    Yeah, somehow I added the envvars. I haven't tested yet without the envvars, but this seems to be the error root cause. Thanks all!

    image
  10. added and removed
    Needs: Author Feedback
    on Oct 6, 2023
  11. self-assigned this
    on Oct 10, 2023
  12. joehan commented on Oct 10, 2023

    @joehan
    Member

    Glad that this fixed your issues! I suspect there is a minor underlying issue here - during functions deployment, we try to get the default database(https://github.com/firebase/firebase-tools/blob/master/src/deploy/functions/services/firestore.ts#L12) using getDatabase, which uses an API client that calls firestoreOriginOrEmulator (https://github.com/firebase/firebase-tools/blob/master/src/gcp/firestore.ts#L8)

    I suspect we can prevent this issue by just switching that to firestoreOrigin, but we ought to double check that doing so won't break the emualtor anywhere. @blidd-google do you mind taking a pass through the usage of the firestore functions in that file and sending a PR to switch it over if this is safe?

  13. LouisBensard commented on Dec 7, 2023

    @LouisBensard

    Hello, I'm running into a similar issue. I've never user the emulator but my functions seem to deploy only on the development environment (see warning in code below). See below the result of the firebase deploy --only functions command:

    (node:9312) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    
    === Deploying to 'myproject-prod'...
    
    i  deploying functions
    i  functions: preparing codebase default for deployment
    i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
    i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
    i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
    ✔  functions: required API cloudfunctions.googleapis.com is enabled
    ✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
    ✔  functions: required API cloudbuild.googleapis.com is enabled
    i  functions: Loading and analyzing source code for codebase default to determine what to deploy
     * Serving Flask app 'serving'
     * Debug mode: off
    
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
     * Running on http://127.0.0.1:8081
    Press CTRL+C to quit
    
    127.0.0.1 - - [07/Dec/2023 11:02:26] "GET /__/functions.yaml HTTP/1.1" 200 -
    
    127.0.0.1 - - [07/Dec/2023 11:02:26] "GET /__/quitquitquit HTTP/1.1" 200 -
    
    /bin/sh: line 1:  9316 Terminated: 15          python3.11 "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/firebase_functions/private/serving.py"
    
    

    (After this the functions proceed to deploy)

    I have tried unsetting my environment variables, similar to suggested by @landa but no success. This is how I tried to unset them:

    username@user-MacBook-Pro functions % export FIRESTORE_EMULATOR_HOST=   
    username@user-MacBook-Pro functions % export FIRESTORE_URL=           
    username@user-MacBook-Pro functions % firebase deploy --only functions:handle_dataset_created
    

    I am using 2nd Gen cloud function, written in python. Deployment via the default (apparently dev server) has been successful without issue.

    EDIT: I gave up and moved my Python 2nd Gen functions to Typescript 1st Gen as it doesn't have the 'development environment warning' issue. Hopefully the 2nd Gen functions get fixed.

  14. hasnainv commented on Jan 2, 2024

    @hasnainv

    @Genev-a I'm running into the same issue and I do not have any FIRESTORE_* environment variable set.

  15. segunak commented on Jan 15, 2024

    @segunak

    @Genev-a I'm running into the same issue and I do not have any FIRESTORE_* environment variable set.

    Same issue here. Python Gen 2, no FIRESTORE_* environment variables set. Any attempts to use firebase deploy end with failure and a warning that the development server is being used.

  16. LouisBensard commented on Jan 15, 2024

    @LouisBensard

    @hasnainv @segunak I gave up using Gen 2 function in Python and reverted back to using 1st gen functions written in Typescript. Even aside from this unresolved issue, the documentation online is more robust for the tried and tested 1st gen functions. Good luck!

  17. david-shortman commented on Mar 23, 2024

    @david-shortman

    I'm also getting this issue for the first time specifically when migrating database triggered functions to v2. I've successfully migrated other HTTP triggered v1 functions to v2.

  18. david-shortman commented on Mar 23, 2024

    @david-shortman

    I'm also getting this issue for the first time specifically when migrating database triggered functions to v2. I've successfully migrated other HTTP triggered v1 functions to v2.

    In my case, I needed to add another role to my deployment service agent that allowed reading Cloud Datastore, which is apparently used by v2 database triggered functions.

  19. jimmy0251 commented on Jul 3, 2024

    @jimmy0251

    In my case, I needed to add another role to my deployment service agent that allowed reading Cloud Datastore, which is apparently used by v2 database triggered functions.

    This worked for me as well. I had to add "Cloud Datastore Viewer" role to be specific. I think error message can be improved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions