Firebase Emulator Suiteで起動しているFunctionsから本番のFirestoreにアクセスする

公開日時
更新日時

Firebase Local Emulator Suiteを使うとFunctionsやFirestoreのローカル環境を作ることができる。

ふと、「ローカルのFunctionsを使いつつ、Firestoreだけ本番環境に接続するのはできるのだろうか?」と思い試してみたところできた。

使う機会はほぼないと思うが対応方法をメモしておく。

通常は↓でエミュレータを起動するが、

yarn run firebase emulators:start

functionsのみエミュレータを起動するようにする。

yarn run firebase emulators:start --only functions

この状態でローカルのFunctionsを呼び出すと、firebase-admin SDKは本番のFirestoreにアクセスするようになる。

なお、ローカルから本番のFirestoreに書き込もうとした場合、エミュレータのログに「Google API requested!」が記録される。

i  functions: Beginning execution of "someFunction"
⚠  Google API requested!
   - URL: "https://oauth2.googleapis.com/token"
   - Be careful, this may be a production service.
i  functions: Finished "someFunction" in ~1s

参考


Related #firebase

Firestoreの複合インデックスを削除する

CLI経由で削除する必要があった

Firebase Web SDK v9

_this.auth.addAuthTokenListener is not a function

8.6.5にダウングレードした

Firebase Summit 2021