コンテンツにスキップ

Google Cloud Functionsの作業メモ

はじめに

Google Cloud Functionsは,マイクロサービスの一部として使うことが想定されている.

どの GCP サービスを選ぶ? 正しい選択のための決定木 | Google Cloud Blog

gcloudコマンドの導入

https://cloud.google.com/sdk/docs/quickstart-macos?hl=ja

コードの作成

pip install Flask==1.0.2
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/1.0/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!'

gcloudコマンドによるデプロイ

以下のコマンドでデプロイ

gcloud functions deploy hello_http --runtime python37 --trigger-http

エンドポイントへアクセス

以下のコマンドでエンドポイントを取得

gcloud functions describe hello_http

ログの確認

以下でログを確認

gcloud functions logs read hello_http