Firebase Functions(第2世代)は、内部的にCloud Run上で動作します。そのため、エラーメッセージにCloud Runが出てきます
とのこと
第1世代の関数に設定することで解決しそう
なのでfirebase.jsonを修正する
firebase.jsonの修正
firebase.jsonで関数を第1世代に設定する
Firebase Functionsの第1世代(Gen 1)と第2世代(Gen 2)の主な違いについて
### **1. 実行環境の違い**
– **第1世代(Gen 1):**
– **Google Cloud Functions**をベースにしています。
– Node.jsのサポートバージョンは**Node.js 10、12、14、16、18**です。
– メモリは最大**2GB**まで利用可能です。
– **シングルリクエストモデル**で、1つの関数インスタンスは同時に1つのリクエストのみを処理します。
– **第2世代(Gen 2):**
– **Google Cloud Run**をベースにしています。
– Node.jsのサポートバージョンは**Node.js 16、18**です。
– メモリは最大**16GB**まで利用可能です。
– **同時実行**が可能で、1つの関数インスタンスが複数のリクエストを同時に処理できます。
### **2. スケーリングとパフォーマンス**
– **第1世代:**
– スケーリングは自動ですが、**コールドスタート**が発生しやすいです。
– 最大インスタンス数はデフォルトで**1000**です。
– **第2世代:**
– スケーリングがより柔軟で、コールドスタートの影響が少ないです。
– 最大インスタンス数はデフォルトで**1000**ですが、同時実行により効率が向上します。
### **3. トリガーのサポート**
– **第1世代:**
– **すべてのFirebaseトリガー**
(Cloud Firestore、Realtime Database、Authentication、Storageなど)
をサポートしています。
– **第2世代:**
– **一部のトリガーのみ**サポートしています。
現在はHTTPトリガーやCloud Eventに対応していますが、他のトリガーは順次対応予定です。
### **4. 設定とカスタマイズ**
– **第1世代:**
– 設定オプションが限定的で、シンプルな設定が可能です。
– タイムアウトは最大**540秒(9分)**まで設定可能です。
– **第2世代:**
– **より詳細な設定**が可能です。タイムアウトは最大**3600秒(1時間)**まで設定できます。
– **VPC接続**や**同時実行数の設定**など、高度なカスタマイズが可能です。
### **5. 料金体系**
– **第1世代:**
– **無料枠**があり、リクエスト数や実行時間に応じて課金されます。
– **コールドスタート**が多い場合、待機時間も考慮する必要があります。
– **第2世代:**
– Cloud Runの料金体系に基づきます。
– 同時実行が可能なため、**コスト効率が向上**する場合があります。
### **6. 環境変数の取り扱い**
– **第1世代:**
– 環境変数は`functions.config()`を使用して取得します。
– `process.env`から直接取得することは推奨されていません。
– **第2世代:**
– 環境変数は`process.env`から直接取得できます。
– `.env`ファイルを使用した環境変数の管理も可能です。
### **7. デプロイと開発の違い**
– **第1世代:**
– 開発とデプロイがシンプルで、**迅速な開発**が可能です。
– Firebase CLIで簡単にデプロイできます。
– **第2世代:**
– **より高度な設定**が可能ですが、その分デプロイ手順が複雑になる場合があります。
– `firebase.json`で世代を指定する必要があります。
– **第1世代(Gen 1)は、シンプルで素早くデプロイしたい場合に適しています。**
– **第2世代(Gen 2)は、高いパフォーマンスや高度なカスタマイズが必要な場合に適しています。**
**参考情報:**
– [Firebase公式ドキュメント – 第1世代と第2世代の比較](https://firebase.google.com/docs/functions/compare-gen-1-and-gen-2?hl=ja)
– [Firebase Functionsの概要](https://firebase.google.com/docs/functions?hl=ja)
これらを元に書き換える
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | { "functions" : [ { "source" : "プロジェクト名" , "codebase" : "プロジェクト名" , "runtime" : "nodejs18" , "ignore" : [ "node_modules" , ".git" , "firebase-debug.log" , "firebase-debug.*.log" , "*.local" ] } ], "functions.deployment" : { "line-bot" : { "gen" : 1 } } } |
というようにした
これで再度
1 | firebase deploy --only functions |
を実行すると
1 2 3 4 5 6 7 8 9 10 11 | ╭────────────────────────────────────────────────────────────────────╮ │ │ │ Update available 13.18.0 → 13.19.0 │ │ To update to the latest version using npm, run │ │ npm install -g firebase-tools │ │ For other CLI management options, visit the CLI documentation │ │ (https: //firebase .google.com /docs/cli #update-cli) │ │ │ │ │ │ │ ╰────────────────────────────────────────────────────────────────────╯ |
この後に
Index.js を編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | const functions = require( "firebase-functions" ); const express = require( "express" ); const line = require( "@line/bot-sdk" ); const config = { channelAccessToken: functions.config().line.channel_access_token, channelSecret: functions.config().line.channel_secret, }; const app = express(); app.post( "/webhook" , line.middleware(config), (req, res) => { Promise.all(req.body.events.map(handleEvent)) . then ((result) => res.json(result)) .catch((err) => { console.error(err); res.status(500).end(); }); }); const client = new line.Client(config); function handleEvent(event) { if (event. type !== "message" || event.message. type !== "text" ) { return Promise.resolve(null); } return client.replyMessage(event.replyToken, { type : "text" , text: event.message.text, }); } exports.webhook = functions.https.onRequest(app); |
として保存し
1 | firebase deploy --only functions |
を実行
webhookのURLが判明するので
これをLINEアカウントにログインして設定する