目次
Djangoプロジェクトを作成
DjangoをGAEにデプロイする公式サイトは以下のリンクです。
Djangoのプロジェクト作成に関しては、以下の記事で紹介しています。
ただプロジェクトを作成するだけでなく、データベース設定などは環境ごとに分割するための手順なども紹介しているので、ぜひ読んでみてください!
GCP側のプロジェクト準備・作成
gcloud-sdkのインストール
$ $ brew cask install google-cloud-sdk
プロジェクトの作成
GCP上でプロジェクトを作成し、プロジェクトIDを確認します。
必要なファイルを用意
.gitignore
secret.yaml
secret.yaml
env_variables:
DB_NAME: '[ データベース名 ]'
DB_HOST: '/cloudsql/[ プロジェクトID ]'
DB_PORT: '3306'
DB_USERNAME: ' '
DB_PASSWORD: ' '
app.yaml
以下のリンクが書き方の公式ページになっているので、参考にしながら進めてください。
Python3 ランタイム環境
このファイルには、プロジェクトを本番環境にデプロイした際に、以下の設定を記述すると思ってください。
・何をエントリーポイントにするか?
・どのURLからのアクセスを有効にするか?
runtime: python37
entrypoint: gunicorn -b :$PORT config.wsgi:application
handlers:
- url: .*
script: auto
ここで、gunicornというライブラリが必要なので、インストールします。
$ pipenv install gunicorn
requirements.txt
現段階では、GAEはPipfileに対応してないので以下のコマンドで、
Pipfileからrequirements.txtを生成します。
requirements.txtとは、これに書かれているライブラリが本番環境でインストールされます。
必要なものをインストールしてくださいね、っていう命令をするためのファイルだと思ってください。
$ pipenv lock -r > requirements.txt
.gcloudignore
また、PipfileをGAEにデプロイした際に一緒にあげてしまわないように、
.gcloudignoreというファイルをプロジェクト直下に作成し、以下のように記述してください。
コピペで大丈夫です。
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg
Pipfile
Pipfile.lock
ホストを許可
ALLOWED_HOSTS = ['*']
デプロイ
$ gcloud auth login
・・・ Your current project is [GCP上で作成したプロジェクトID]・・・
プロジェクトIDは、Google Cloud Platformから確認できます。
すると、以下のように表示されるので、2を入力します。
$ gcloud app deploy
Please choose the region where you want your App Engine application
located:
1] asia-east2 (supports standard and flexible)
2] asia-northeast1 (supports standard and flexible)
3] asia-northeast2 (supports standard and flexible)
4] asia-south1 (supports standard and flexible)
[5] australia-southeast1 (supports standard and flexible)
[6] europe-west (supports standard and flexible)
[7] europe-west2 (supports standard and flexible)
[8] europe-west3 (supports standard and flexible)
[9] europe-west6 (supports standard and flexible)
[10] northamerica-northeast1 (supports standard and flexible)
[11] southamerica-east1 (supports standard and flexible)
[12] us-central (supports standard and flexible)
[13] us-east1 (supports standard and flexible)
[14] us-east4 (supports standard and flexible)
[15] us-west2 (supports standard and flexible)
[16] cancel
Please enter your numeric choice:
続いて、以下ではYを入力します。
Do you want to continue (Y/n)?
少し時間がかかりますが、うまくいけばデプロイ完了です。
ブラウザで表示
$ gcloud app browse –project=[プロジェクトID]
ログを確認
もし、エラーが出た場合は、以下のコマンドでログを確認してみましょう。
$ gcloud app logs tail –project=[プロジェクトID]
Cloud SQL ProxyでローカルからCloud SQLに接続
・MySQLインスタンスを作成
・Cloud SQL Admin APIを有効化
・Cloud SQL Proxyで接続(ローカルからマイグレーションを行うため)
MySQLインスタンスを作成
Cloud SQL APIを有効化
Cloud SQL APIを有効化することで、GCP内のプロジェクトからCloud SQLに接続を可能にします。

cloud_sql_proxyを取得
$ curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
$ chmod +x cloud_sql_proxy
プロキシを開始

[インスタンス接続名] は、
SQL > インスタンス名 > このインスタンスとの接続 > 接続名
構成は、project:region:instance-nameとなっています。
$ ./cloud_sql_proxy -instances=[インスタンス接続名]=tcp:5306
2019/12/29 16:36:22 Listening on 127.0.0.1:3306 for [インスタンス接続名]
2019/12/29 16:36:22 Ready for new connections
上記のように表示されれば、接続完了です!
ん?これで何ができるようになっているのか??
簡単にいうと、ローカル環境とCloud SQLをつなぐことができました。
つまり、ローカルからCloud SQLを操作できる環境が整った、ということです。
ポート番号に関しては、ローカルPCで使用していないポート番号を指定します。MySQLをインストールしている方は、おそらく3306ではすでにローカルのMySQLが動いているかもしれないので、3306以外で接続するといいと思います。
....current FDs rlimit set to 10240, wanted limit is 8500. Nothing to do here. ....listen tcp 127.0.0.1:3306: bind: address already in use
ローカルからCloud SQLに接続するユーザー作成
Cloud MySQLを起動します。その際、ユーザー名とパスワード(任意)が必要なので、まずは作成します。

ローカルからCloud SQLに接続
$ mysql -u [ユーザー名] -p –host 127.0.0.1 -P 5306
ローカルからマイグレーションを実行
$ pipenv run python manage.py migrate --settings=config.settings.production