概要
Googleさんからサーバーレス関連のサービス強化がアナウンス(2018/08/03)されました。
そのなかで、Cloud Functionsも正式提供やBetaで機能強化されています。
サーバーレス コンピューティングの実現に向けて
https://cloudplatform-jp.googleblog.com/2018/08/bringing-the-best-of-serverless-to-you.html
また、イベント駆動型のコンピュート サービスである Cloud Functions の正式提供も開始しました。Cloud Functions では予測可能なサービスが SLA で保証されるようになり、グローバル ネットワーク フットプリントを利用して提供され、欧州とアジアでリージョンも追加されました。さらに、Python 3.7 と Node.js 8 のサポートや、ネットワーキングとセキュリティの管理など、お客様からの要望が高かった新機能で強化されており、全体的なパフォーマンスも向上しています。Cloud Functions は、BigQuery、Cloud Pub/Sub、機械学習 API、G Suite、Google アシスタント など 20 以上の GCP サービスをシームレスに接続して拡張できます。
というわけで、Cloud FunctionsでPython 3.7が対応(Beta)されたとのことで、早速利用してみました。
準備
GCP プロジェクトの選択・作成やAPIの有効化などは実施済みの前提です。
まだの方は下記の「始める前に」をご参考ください。
https://cloud.google.com/functions/docs/tutorials/http?hl=ja
管理コンソールから関数を作成する
Cloud Functionsの[関数を作成]ボタンをポチッと。
[ランタイム]プルダウンメニューをみると[Python 3.7]が選べます!
ただし[(Beta)]がついてます。Node.js 8もしれっと増えてます。
名前は任意で入力してください。
今回はトリガーを[HTTP]でサンプルそのままで関数を作成してみます。
関数作成中。Stackdriver Loggingでログも確認できます。
管理コンソールでテストしてみた。動いてますね^^
CurlでGETしてみた。
URLは[トリガー]から確認できます。
1 2 | > curl https: //us-central1-xxx .cloudfunctions.net /cloud-functions-python-test Hello World! |
パラメータも受け取ってくれました。
1 2 3 | > curl "https: //us-central1-xxx .cloudfunctions.net /cloud-functions-python-test ?message=h oge" hoge |
お手軽ですね^^
gcloudコマンドラインツールから利用してみる
続いてはコマンドラインツールから試してみます。
Google Cloud SDKのインストール
インストールがまだの方は下記をご参考ください。gcloud init
まで完了した前提です。
MacでCloud Machine Learning Engineを利用してみる
https://cloudpack.media/42306
1 2 3 4 5 6 7 8 9 | > gcloud --version Google Cloud SDK 209.0.0 bq 2.0.34 core 2018.07.16 gsutil 4.33 Updates are available for some Cloud SDK components. To install them, please run: $ gcloud components update |
gcloud
コマンドが利用できるようになったらCloud Functions関連のコマンドをためてしてみます。
gcloud functions
コマンドは209.0.0 (2018-07-18)でGAとなったようですね。
バージョンが古い方は、各自、自己責任でgcloud components update
してください^^
Google Cloud SDK – Release Notes
https://cloud.google.com/sdk/docs/release-notes
Google Cloud SDK – Release Notes
https://cloud.google.com/sdk/docs/release-notes
チュートリアルに沿ってデプロイまでやってみる
公式ドキュメントを参考にしてデプロイできるところまで進めてみます。
残念ながら、まだ言語はNode.jsだけでした。(2018/08/07現在)
HTTP のチュートリアル
https://cloud.google.com/functions/docs/tutorials/http?hl=ja
※ページの言語設定(ページ左下)を英語に変更するとNode.js8とPythonも追加されています。@uu4k さん情報あざます!(2018/08/20追記)
コードの準備
関数を作成するのにコードを用意します。
実装は上記でも利用した公式のサンプルをそのまま利用します。
今回はサンプルなので、pip
でライブラリをインストールすることはないですが、念の為、仮想環境を作っておきます。
仮想環境ってなに?という方は下記をご参考。
Pythonもローカル実行しないのであれば、3系でvenv
が利用できたらなんでも。
Macでanyenvをつかってpython環境構築(bash、fish対応)
https://cloudpack.media/42033
仮想環境の作成
1 2 3 4 5 6 7 | > mkdir 任意のディレクトリ > cd 任意のディレクトリ > python -m venv function - test > . function - test /bin/activate # fishの方はこちら > . function - test /bin/activate .fish |
1 2 3 | main.pyの作成 > touch main.py > vi main.py |
main.py
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | def hello_world(request): """Responds to any HTTP request. Args: request (flask.Request): HTTP request object. Returns: The response text or any set of values that can be turned into a Response object using `make_response <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>`. """ request_json = request.get_json() if request.args and 'message' in request.args: return request.args.get('message') elif request_json and 'message' in request_json: return request_json['message'] else: return f'Hello World!' |
関数をデプロイする前に
Python 3.7はまだBeta提供なので、gcloud functions deploy
コマンドでデプロイすることができませんでした。(2018/08/7時点)
1 2 3 4 | > gcloud functions deploy hello_world --trigger-http Deploying function (may take a while - up to 2 minutes)...failed. ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File index.js or function .js that is expected to define function doesn't exist in the root directory. |
なので、gcloud beta
コマンドが利用できるようにします。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | > gcloud beta You do not currently have this command group installed. Using it requires the installation of components: [beta] Your current Cloud SDK version is: 210.0.0 Installing components from version: 210.0.0 ┌─────────────────────────────────────────────┐ │ These components will be installed. │ ├──────────────────────┬────────────┬─────────┤ │ Name │ Version │ Size │ ├──────────────────────┼────────────┼─────────┤ │ gcloud Beta Commands │ 2018.07.16 │ < 1 MiB │ └──────────────────────┴────────────┴─────────┘ For the latest full release notes, please visit: https://cloud.google.com/sdk/release_notes Do you want to continue (Y/n)? (略) For detailed information on this command and its flags, run: gcloud beta --help |
インストールできたらコマンドが利用できるか確認します。
1 2 3 4 5 6 7 | > gcloud --version Google Cloud SDK 210.0.0 beta 2018.07.16 bq 2.0.34 core 2018.07.27 gsutil 4.3 |
関数をデプロイする
gcloud beta
コマンドが利用できるようになったら、[HTTP]トリガーでデプロイしてみます。gcloud beta functions deploy
コマンドのパラメータは--help
か下記をご参考。--runtime
オプションがありました^^
gcloud beta functions deploy
https://cloud.google.com/sdk/gcloud/reference/beta/functions/deploy?authuser=19&hl=ja
ではデプロイしてみます。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | > gcloud beta functions deploy hello_world --trigger-http --runtime=python37 Deploying function (may take a while - up to 2 minutes)... done . availableMemoryMb: 256 entryPoint: hello_world httpsTrigger: url: https: //us-central1-xxx .cloudfunctions.net /hello_world labels: deployment-tool: cli-gcloud name: projects /xxx/locations/us-central1/functions/hello_world runtime: python37 (略) status: ACTIVE timeout: 60s updateTime: '2018-08-07T03:07:17Z' versionId: '3' |
デプロイが完了しましたので、HTTPアクセスしています。
1 2 3 4 5 | > curl https: //us-central1-xxx .cloudfunctions.net /hello_world Hello World! > curl "https://us-central1-xxx.cloudfunctions.net/hello_world?message=hoge" hoge |
はい。
動作していますね^^
その他コマンド
list
コマンドはbetaでもかわりなし。
1 2 3 4 5 6 7 8 9 | > gcloud functions list NAME STATUS TRIGGER REGION cloud-functions-python- test ACTIVE HTTP Trigger us-central1 hello_world ACTIVE HTTP Trigger us-central1 > gcloud beta functions list NAME STATUS TRIGGER REGION cloud-functions-python- test ACTIVE HTTP Trigger us-central1 hello_world ACTIVE HTTP Trigger us-central1 |
describe
コマンドはupdateTime
とversionId
が追加されてました。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | > gcloud functions describe hello_world availableMemoryMb: 256 entryPoint: hello_world httpsTrigger: url: https: //us-central1-xxx .cloudfunctions.net /hello_world labels: deployment-tool: cli-gcloud name: projects /xxx/locations/us-central1/functions/hello_world runtime: python37 serviceAccountEmail: (略) sourceUploadUrl: (略) timeout: 60s > gcloud beta functions describe hello_world availableMemoryMb: 256 entryPoint: hello_world httpsTrigger: url: https: //us-central1-xxx .cloudfunctions.net /hello_world labels: deployment-tool: cli-gcloud name: projects /xxx/locations/us-central1/functions/hello_world runtime: python37 serviceAccountEmail: (略) sourceUploadUrl: (略) status: ACTIVE timeout: 60s updateTime: '2018-08-07T03:07:17Z' versionId: '3' |
試し終わったらdelete
コマンドで関数を削除しておきましょう。
管理コンソールから作成した関数もお忘れなく^^
こちらもランタイムがPythonでも、Betaじゃなくても削除できました。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | > gcloud functions delete cloud-functions-python- test Resource [projects /xxx/locations/us-central1/functions/ cloud-functions-python- test ] will be deleted. Do you want to continue (Y /n )? Y Waiting for operation to finish... done . Deleted [projects /xxx/locations/us-central1/functions/cloud-functions-python-test ]. > gcloud beta functions delete hello_world Resource [projects /xxx/locations/us-central1/functions/ hello_world] will be deleted. Do you want to continue (Y /n )? Y Waiting for operation to finish... done . Deleted [projects /xxx/locations/us-central1/functions/hello_world ]. |
これまでnode.jsしか対応していなかったのが、Pythonが増えて、さらに便利になってきましたね。
では良きサーバレスライフを^^