まらんさんのチラ裏

その日暮らしのおじさん

別プロジェクトの firebase functions を呼び出す

firebase も GCP もなんもわからん。 ってなってたんですが、とりあえずなんとかする方法がわかったので雑にメモ。

  • Project A: 呼び出される側
  • Project B: 呼び出す側

Project A 側

A に https.onCall な function testFunc を実装する。

export const testFunc = functions
  .https.onCall(async (data) => {
  functions.logger.debug('よばれたよー');
};

GCP コンソール

A の testFunc の権限に B の IAM ユーザを Cloud Functions 起動元 として追加する。

Project B 側

B から httpsCallable で A の testFunc を呼び出す。

export const callTestFunc = functions
  .https.onCall(async (data) => {
    const firebaseApp = firebase.initializeApp({
      credential: admin.credential.applicationDefault(),
    });
    await firebaseApp
      .functions(PROJECT_A_FUNCTIONS_ROOT_URL)
      .httpsCallable('testFunc')(params);
  });

これで呼べるはず。