見出し画像

超簡単PythonでGoogle App Engine入門(Hello World)

Google App Engineで超簡単にPythonでhello World

1. プロジェクト作成

Google Cloud Consoleからプロジェクト作成

FireShot Capture 065 - ダッシュボード – ホーム – Google Cloud Platform - console.cloud.google.com

FireShot Capture 066 - 新しいプロジェクト – Google Cloud Platform - console.cloud.google.com

FireShot Capture 067 - ホーム – My Project 20291 – Google Cloud Platform - console.cloud.google.com

2. Cloud Build API有効化

メニューの「APIとサービス」からCloud Build API有効化

FireShot Capture 092 - API とサービス – API とサービス – My Project 20291 – Google Cloud Platform_ - console.cloud.google.com

FireShot Capture 093 - API ライブラリ - My Project 20291 - Google Cloud Platform_ - console.cloud.google.com

FireShot Capture 094 - Cloud Build API – API とサービス – My Project 20291 – Google Cloud Platfor_ - console.cloud.google.com

FireShot Capture 095 - 概要 – API とサービス – My Project 20291 – Google Cloud Platform_ - console.cloud.google.com

3. Google Cloud SDK のインストールと初期化

下記のドキュメントを参考に環境(Mac・Windows・その他)に合わせてGoogle Cloud SDK のインストールと初期化を行う

4. App Engineアプリ作成&リージョン選択

$ gcloud app create --project=[YOUR_PROJECT_ID]

リージョンはasia-northeast1(東京)もしくはasia-northeast2(大阪)を選択

5.  gcloud コンポーネントをインストール

$ gcloud components install app-engine-python

6. ファイル作成

$ mkdir hello-world && cd hello-world
$ python3 -m venv .venv
$ source .venv/bin/activate

hello-world/main.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
   return 'Hello World!'

if __name__ == '__main__':
   app.run(host='127.0.0.1', port=8080, debug=True)

hello-world/app.yaml

runtime: python39

hello-world/requirements.txt

Flask==2.0.1

7. ローカル実行

(.venv) $ pip install -r requirements.txt
(.venv) $ python main.py

ブラウザにてhttp://127.0.0.1:8080/確認

8. App Engine にデプロイ

$ gcloud app deploy

9. 確認

$ gcloud app browse

またはhttps://PROJECT_ID.REGION_ID.r.appspot.comへアクセス

以上、超簡単!

10. 参考

11. 関連記事



この記事が気に入ったらサポートをしてみませんか?