LoginSignup
13
11

More than 5 years have passed since last update.

Cloud Functions for Firebaseに関する雑記

Last updated at Posted at 2018-01-28

概要

Cloud Functions for Firebase(β版)を使ってみた雑記
ちなみに使ってみたのはhttp(s)のトリガーのみ
※ 書いてあることは適当なので鵜呑みにしないように

準備

使い方は簡単

firebase consoleでプロジェクトをさくせいして、プロジェクトのディレクトリで以下のコマンドを叩くだけ。

$ firebase init

functionsを選択すると./functionsディレクトリにひな形ファイルが生成される。

コンソールの結果


     🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥     🔥🔥🔥     🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥
     🔥🔥        🔥🔥  🔥🔥     🔥🔥 🔥🔥       🔥🔥     🔥🔥  🔥🔥   🔥🔥  🔥🔥       🔥🔥
     🔥🔥🔥🔥🔥🔥    🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥   🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥
     🔥🔥        🔥🔥  🔥🔥    🔥🔥  🔥🔥       🔥🔥     🔥🔥 🔥🔥     🔥🔥       🔥🔥 🔥🔥
     🔥🔥       🔥🔥🔥🔥 🔥🔥     🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥 🔥🔥🔥🔥🔥🔥🔥🔥  🔥🔥     🔥🔥  🔥🔥🔥🔥🔥🔥  🔥🔥🔥🔥🔥🔥🔥🔥

You're about to initialize a Firebase project in this directory:

  /hogehoge/test-firebase-functions

? Which Firebase CLI features do you want to setup for this folder? Press Space to select features, then Enter to confirm your choices. Functions: Configure and deploy Cloud Functions

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.

? Select a default Firebase project for this directory: hogehoge (hogehoge)

=== Functions Setup

A functions directory will be created in your project with a Node.js
package pre-configured. Functions can be deployed with firebase deploy.

? What language would you like to use to write Cloud Functions? JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style? No
✔  Wrote functions/package.json
✔  Wrote functions/index.js
? Do you want to install dependencies with npm now? Yes

> grpc@1.7.3 install /hogehoge/test-firebase-functions/functions/node_modules/grpc
> node-pre-gyp install --fallback-to-build --library=static_library

[grpc] Success: "/hogehoge/test-firebase-functions/functions/node_modules/grpc/src/node/extension_binary/node-v59-darwin-x64-unknown/grpc_node.node" is installed via remote

> protobufjs@6.8.4 postinstall /hogehoge/test-firebase-functions/functions/node_modules/google-gax/node_modules/protobufjs
> node scripts/postinstall

npm notice created a lockfile as package-lock.json. You should commit this file.
added 384 packages in 23.892s

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...

✔  Firebase initialization complete!

実装

ここで作成された雛形ファイルを見てみよう。

./functions/index.js
const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

上記のコメントアウトを外すとhttp(s)でリスエストするとHello from Firebase!と返すFunctionが作成される。
簡単だ。

デプロイ

おなじみのコマンドうつだけ

$ firebase deploy

動作確認

firebase consoleのFunctionsのリストにURLが表示されているのでそれを叩く

$ curl https://hogehoge.cloudfunctions.net/helloWorld
Hello from Firebase!

所感

  • コマンド2回叩くだけでFaaSが実行できるのはすごい楽
  • AWS Lambdaと比べて前提知識がほとんど必要ないのはとても良い
    • Lambdaの場合はAPI Gatewayとつなげたりとか色々めんどかった

Tips

無料枠(Sparkプラン)だと外部API叩けない

TwitterのAPIを叩くFunctionを作ったけど無理だった。以下のエラーが表示される。

Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

Flameプラン($25/月)で行けるのだろうか。だれか知ってたら教えて。
※ ちゃんとしれべてないけど、stackoverflowとかにBlazeプランじゃないとダメだよって書いてあった気がする
→ 教えてもらいました。Flameプランでも行けます。

その他

firebase console側で実行するリージョン選ぶような操作必要だったかも。

13
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11