Minimal Flask app on Google App Engine
November 15, 2017
appengine
flask
Feeling overwhelmed by all Google Cloud Platform documentation? Have no fear. Here is how to create and deploy the simplest possible Flask app running on Google App Engine standard environment in three easy steps.
Step 1 - Setup Google Cloud Platform
First make sure that you have a working installation of Python 2 and pip
.
Install Google Cloud SDK on your computer and
register for the Google Cloud Platform Free
Trial. Before proceeding to Step
2 make sure that gcloud
and dev_appserver.py
commands are available on the
command line.
Step 2 - Write the app
Create some boilerplate first.
# create a folder to host the app
$ mkdir simple && cd simple
# add Flask to the requirements file
$ echo Flask==0.12.1 > requirements.txt
# install the requirements
$ pip install -t lib -r requirements.txt
Create main.py
file with the following contents.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello google app engine!'
Create appengine_config.py
with the following contents. Needed for
external dependencies.
from google.appengine.ext import vendor
vendor.add('lib')
Finally add app.yaml
which is the configuration file.
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
Test run the app locally.
$ dev_appserver.py .
You should now be able to access it.
$ curl localhost:8080
Step 3 - Deploy the app
Lets now deploy the app and see it run live in the cloud.
# login to be able to deploy the app
$ gcloud auth login
# create new GCP project (change name here)
$ export PROJECT_ID=simple-gae-project-2134
$ gcloud projects create $PROJECT_ID
# set this project to current project
$ gcloud config set project $PROJECT_ID
# check your config
$ gcloud config list
# you need to create the app first in the specific region.
# omit the region to choose it interactivelly.
$ gcloud app create --region=us-east1
# now deploy the code
$ gcloud app deploy
# open app in the browser
$ gcloud app browse
Done. That’s all there is to it.