0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python Cloud functions をローカル環境で実行する

Last updated at Posted at 2019-04-11

最近cloud functionsを使っていてローカルでデバッグしたかったので。
自分用覚え書き

pipenv使ってる人はflaskインストールします
環境変数使ってる方はpyyamlも

pipenv install --dev flask, pyyaml

ついでにpipfileにscriptsも追加します

Pipfile
[scripts]
dev = "python main.py"

こんな感じで追加

main.py
if __name__ == "__main__":
    from flask import Flask, request
    import yaml
    app = Flask(__name__)

    # 環境変数読み込み
    with open('.env.yaml', 'r') as f:
        ENV = yaml.load(f)
    MY_TOKEN = ENV.get('MY_TOKEN')

    @app.route('/', methods=['OPTIONS', 'POST', 'GET])
    def index():
        return my_function(request)
    app.run('127.0.0.1', 8000, debug=True)

サーバー起動して簡単に確認できちゃいます

pipenv run dev

deployもこんな感じでbashスクリプト作っておけば簡単

deploy.sh
GCF_NAME="my-func-name"
PROJECT_ID="my-project-id"
GCF_REGION="asia-northeast1"

gcloud functions deploy $GCF_NAME \
  --runtime python37 \
  --region $GCF_REGION \
  --trigger-http \
  --env-vars-file .env.yaml

もっと楽ちんな方法あったら誰か教えてくださ〜い

0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?