Cloud Functions for Firebaseとは

Firebaseが提供する、サーバレスで関数実行のできるサービスです。
AWSLambdaGoogle Cloud Functionsが有名で、
これを使用することでサーバレスアーキテクチャを実現できます。
雑に言うと、「サーバを借りなくてもサーバ上でアプリケーションが実行できる」ということです。

何らかのイベントを契機に関数を実行できることから、FaaSと呼ばれるそうです。
サーバレスアーキテクチャについてはこちらのQiitaの記事がわかりやすいと思います。



FirebaseプロジェクトでCloud Functions for Firebaseを設定する

firebaseプロジェクト内まで移動して初期化を実行します。

$ firebase init

     ######## #### ########  ######## ########     ###     ######  ########
     ##        ##  ##     ## ##       ##     ##  ##   ##  ##       ##
     ######    ##  ########  ######   ########  #########  ######  ######
     ##        ##  ##    ##  ##       ##     ## ##     ##       ## ##
     ##       #### ##     ## ######## ########  ##     ##  ######  ########

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

  (Firebaseプロジェクトのパス)

Before we get started, keep in mind:

  * You are initializing in an existing Firebase project directory

? 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.

i  .firebaserc already has a default project, skipping

=== 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? TypeScript
? Do you want to use TSLint to catch probable bugs and enforce style? Yes
✔  Wrote functions/package.json
✔  Wrote functions/tslint.json
✔  Wrote functions/tsconfig.json
✔  Wrote functions/src/index.ts
✔  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes
npm notice created a lockfile as package-lock.json. You should commit this file.
added 8 packages from 6 contributors, removed 88 packages and audited 4215 packages in 9.714s
found 0 vulnerabilities


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

✔  Firebase initialization complete!

Firebaseプロジェクトの中にfunctionsというディレクトリが作成されます。
initするときにJavaScriptとTypeScriptのどちらを使用するか選べますが、
今回はTypeScriptにしてみました。

index.tsを開くとimportの1文とコメントアウトされたサンプル関数がありますので、
一部をコメントインして下記のようにします。

import * as functions from 'firebase-functions';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {firebase deploy --only functions
 response.send("Hello from Firebase!");
});

保存したら、Cloud Functionsをデプロイします。
今回は特にオプションを指定せずにデプロイコマンドを実行してみましたが、
Cloud Functionsのみをデプロイする場合は $ firebase deploy --only functions を使用します。

$ firebase deploy


=== Deploying to '(Firebaseプロジェクト名)'...

i  deploying database, functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint (プロジェクトのパス)/functions
> tslint --project tsconfig.json

Running command: npm --prefix "$RESOURCE_DIR" run build

> functions@ build (Firebaseプロジェクトのパス)/functions
> tsc

✔  functions: Finished running predeploy script.
i  database: checking rules syntax...
✔  database: rules syntax for database hamople-6a889 is valid
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (51.11 KB) for uploading
✔  functions: functions folder uploaded successfully
i  database: releasing rules...
✔  database: rules for database (Firebaseプロジェクト名)released successfully
i  functions: creating Node.js 6 function helloWorld(us-central1)...
✔  functions[helloWorld(us-central1)]: Successful create operation.
Function URL (helloWorld): (URL)/helloWorld

✔  Deploy complete!

Project Console: (URL)/overview

Firebaseのコンソールを開くと、関数が1つ表示されています。

HTTPでリクエストができるので、表示されているURLをブラウザにコピーします。
デプロイが成功していると、「Hello from Firebase!」とブラウザに表示されます。

あとは、コメントインしたした関数部分を書き換えれば自由に関数実行できます。
また使いこなせるようになったら記事にしたいと思います。

スポンサーリンク