LoginSignup
25

More than 5 years have passed since last update.

Cloud Functions for Firebaseで東京リージョンを利用する

Posted at

はじめに

Cloud Functionsでheadless Chromeが動くようになったよー。
ということで、これまでherokuで動かしていたwebスクレーピングをCloud Functionsに引っ越しさせました。
Introducing headless Chrome support in Cloud Functions and App Engine

しかし日本国内のwebサイトをスクレーピングする場合、かなりレスポンスが悪かったため東京リージョンを利用してみました。
(何も指定せずにデプロイするとus-central1になります。)

環境

Firebase CLI 5.1.1
Google Cloud SDK 220.0.0

東京リージョンを利用する方法

関数を東京リージョンにデプロイするにはいくつかの方法があります。
なお、デプロイ後にリージョンのみを変更することはできません(これはGCPでも同じ)。

ソースコード上でリージョンを指定する

Firebaseのドキュメントではこの方法しか見当たりませんでした。
関数のデプロイとランタイム オプションを管理する | Firebase

記載されている通り、以下のようにリージョンを指定することでデプロイすることができます。

index.js
exports.sample = functions.region('asia-northeast1')
        .https.onRequest((req, res) => {
    console.log('hello tokyo reagion');
});

コンソールで確認するとリージョンがasia-northeast1になって
います :raised_hands:
スクリーンショット 2018-10-17 23.36.40.png

複数のリージョンにデプロイする

GCPのCloud Functionsでは同一関数名であっても、別リージョンであればデプロイすることが可能です。

Cloud Functions Locations
You can deploy functions to different regions within a project, but once the region has been selected for a function it cannot be changed.
Functions in a given region in a given project must have unique (case insensitive) names, but functions across regions or across projects may share the same name.

Firebaseで東京リージョンにデプロイしたあと、リージョン指定を消してデプロイするとどうなるのか?
デプロイしようとすると、以下のような注意が促されます。

The following functions are found in your project but do not exist in your local source code:
    sample(asia-northeast1)

If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify

? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)

そのまま進めると、GCPと同様に複数のリージョンに同じ関数をデプロイすることができました。
スクリーンショット 2018-10-18 0.10.10.png

Google Cloud SDKを使用する

Google Cloud SDKにはオプションとしてリージョン指定が用意されています。

Firebase CLIにはそのオプションが存在しないようなので、ソースコードに手を加えることなく、複数のリージョンにデプロイする場合、この方法を取るしかないのかなと…。

$ gcloud functions deploy --help
NAME
    gcloud functions deploy - create or update a Google Cloud Function

SYNOPSIS
    gcloud functions deploy (NAME : --region=REGION)
        [--entry-point=ENTRY_POINT] [--memory=MEMORY] [--retry]
        [--runtime=RUNTIME] [--source=SOURCE] [--stage-bucket=STAGE_BUCKET]
        [--timeout=TIMEOUT] [--update-labels=[KEY=VALUE,...]]
        [--clear-labels | --remove-labels=[KEY,...]]
        [--trigger-bucket=TRIGGER_BUCKET | --trigger-http
          | --trigger-topic=TRIGGER_TOPIC
          | --trigger-event=EVENT_TYPE --trigger-resource=RESOURCE]
        [GCLOUD_WIDE_FLAG ...]


$ firebase deploy --help
Usage: deploy [options]

deploy code and assets to your Firebase project

Options:
  -p, --public <path>      override the Hosting public directory specified in firebase.json
  -m, --message <message>  an optional message describing this deploy
  -f, --force              delete Cloud Functions missing from the current working directory without confirmation
  --only <targets>         only deploy to specified, comma-separated targets (e.g. "hosting,storage"). For functions, can specify filters with colons to scope function deploys to only those functions (e.g. "--only functions:func1,functions:func2"). When filtering based on export groups (the exported module object keys), use dots to specify group names (e.g. "--only functions:group1.subgroup1,functions:group2)"
  --except <targets>       deploy to all targets except specified (e.g. "database")
  -h, --help               output usage information

さいごに

結局Firebaseではなく直接GCPのCloud Functionsを使ってしまったほうが手っ取り早いんじゃないかという結論に…

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
25