🐳

Google Cloud Functions Emulator使ってみる

2024/12/31に公開

Google Cloud Functionsのemulatorがあるらしいので使ってみます

https://github.com/GoogleCloudPlatform/cloud-functions-emulator

 $ npm install -g @google-cloud/functions-emulator

関数の作成・デプロイ

index.js
exports.helloWorld = function(req, res) {
    console.log(req.body);
    res.status(200).send("hello world");
}

エミュレータにデプロイして、実行してみる

# triggerはhttp triggerとしています
$ functions deploy helloWorld --trigger-http
$ functions call helloWorld
ExecutionId: ab0a9149-013c-4c78-a7b7-392a2b44ff26
Result: hello world

リクエストのデータを送る方法

$ functions call helloWorld --data='{"key"="value"}'

jsonファイルを用意する方法

$ functions call helloWorld --file=./request.json

ログの表示

$ functions logs read

以下を実行すると、Log file場所がわかるのでtailで表示させてもいいです

$ functions status

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Google Cloud Functions Emulator                                                                                                                       │
├──────────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Status           │ RUNNING                                                                                                                            │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Uptime           │ 47 hour(s)                                                                                                                         │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Process ID       │ 82817                                                                                                                              │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ REST Service     │ http://localhost:8008/                                                                                                             │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ gRPC Service     │ http://localhost:8009/                                                                                                             │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ HTTP Triggers    │ http://localhost:8010/xxxxxxxxxxxxxxxxxxx/us-central1/:function                                                                    │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Log file         │ /Users/xxxxxxxxxxxxx/.nvm/versions/node/v8.9.0/lib/node_modules/@google-cloud/functions-emulator/logs/cloud-functions-emulator.log │
├──────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Emulator Version │ 1.0.0-alpha.27                                                                                                                     │
└──────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

If the Emulator becomes unresponsive, kill it will functions kill and then ensure that no other Emulator Node.js processes are running before restarting the Emulator.

デバッグできたり、モックを使ったりもできるのでお試しください

Discussion