Google Cloud Platform(GCP)とは
Googleが提供するクラウドコンピューティングサービス
手順
請求先アカウントの登録
課金が有効になっていない場合、以下のようなダイアログが表示されるので、請求先アカウントを管理から登録する
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/73f3b1a264a84dd09a7ad267fb616048/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.46.55.png)
サイドバーのお支払いから
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/062fadad136d462fb946076382c3ecb9/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.53.39.png)
請求先アカウントをリンクをクリックして登録済みのアカウントを紐付ける
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/7de358cd45da4a75a10cf6941f9054b9/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.54.18.png)
Gcloud CLIのインストール
以下に、os毎にインストール方法が記述されている
Python 3(3.5~3.9)がPCにインストールされていることを確認し、インストーラーを選択してダウンロードする
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/64cc39a9d3e748c182f49ea01017bbe6/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2010.59.32.png)
ダウンロードしたファイルをPCのホームディレクトリで展開するため、ホームディレクトリに移動して、install.shを実行する
./google-cloud-sdk/install.sh
以下の初期化コマンドを打つと、認証やプロジェクトのセットアップなどが完了する
./google-cloud-sdk/bin/gcloud init
プロジェクトの作成
GCPを始めるにはGoogleアカウントが必要になるので事前に作成しておく
GCPアカウント作成
トップページ画面上の「無料で使ってみる」をクリックする
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/e51a64e83a874d8b9c8a50e3e0c61c75/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%209.49.46.png)
次画面で利用規約の同意やアカウント情報を済ませる。なお、最初は無料トライアルとして$300分のクレジットが利用できる
その後、続いて支払い情報、組織名や住所、支払い方法を入力し、「無料トライアルを開始」を押す
ここまででGCPのアカウント作成が完了。
新規プロジェクト作成
プロジェクトにはGCPではGoogle App Engineなどさまざまなサービスを利用できるが、これらサービスを利用するためにはプロジェクトを作成する必要がある。
新しいプロジェクトをクリック
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/dec20020af5f419c845e3e662d556ba2/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2010.52.53.png)
作成するプロジェクト名称を入力し、「作成」を押下すると、プロジェクトが作成される
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/bfd66153659242c8b9b4734ba36ccb69/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2010.54.43.png)
Cloud build APIの有効化
サイドバーのAPIとサービスから有効なAPIとサービスを選択しCloud build APIを有効化する
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/43efe2731d1542e08ead197f6562937d/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.34.46.png)
App Engine アプリの初期化
サイドバーのAPIとサービスからApp Engine
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/25613f490eaf45368ee4d15be8bb20c0/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.40.39.png)
アプリケーションを作成をクリック
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/70dc08832c5247e38d52f9b77f03844d/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.41.33.png)
リージョン: asia-northeast1を選択し、次へ
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/016bb74456cd42989c799d88702985a4/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-22%2011.44.00.png)
gcloud app create --project=[YOUR_PROJECT_ID]
次のコマンドを実行して、Python 用の App Engine 拡張機能が含まれている gcloud コンポーネントをインストール。
gcloud components install app-engine-python
Hello World サンプル アプリケーション リポジトリのクローンをローカルマシンに作成
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
サンプルコードが含まれているディレクトリに移動。
cd python-docs-samples/appengine/flexible/hello_world
hello_world
ディレクトリで次のコマンドを実行して、Hello World アプリをデプロイ
gcloud app deploy
デプロイ時に、python-docs-samples/appengine/flexible/hello_world/app.yamlのenvがflexになっているので、フレキシブル環境にデプロイされるようになっている。
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
operating_system: ubuntu22
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
サービスからApp Engineに作成したプロジェクトが表示されていれば成功です。
![](https://images.microcms-assets.io/assets/c8f47cd9ef654a2d9ca6b6e579be433f/25b607b81d2c4b88a2188520dcecc16c/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-08-24%209.19.23.png)
まとめ
いかがでしたでしょうか。本記事では、Google App Engineのフレキシブル環境にpythonアプリケーションをデプロイする方法について紹介しました。GAEのフレキシブル環境へのデプロイについての情報がネット上にあまりまだ多くはないので、ぜひ参考にしてみて下さい。