Serverless applications with Google App Engine

In my last blog about Serverless Applications with AWS Fargate
we looked at how to create a simple Flask/Python based application in AWS. The application is a Serverless Continuous Running
Application or ‘SCRA’. A SCRA application runs continuously, has virtually no limit on the code size and programming languages
that can be used and you pay only for what you use. Above all, with SCRA applications, you don’t manage servers.

This time we will deploy the application to Google App Engine (GAE). Lets take a look!

Google App Engine

GAE is a platform for serverless applications that provides development and hosting capabilities to
developers. Programs can be developed in most popular programming languages like Python, Go and Java.
GAE provides the infrastructure for running web applications at cloud scale. The architecture of GAE is fixed which
means that GAE is fit for purpose for certain applications like stateful web applications.

Example

The example project consists of the following files,
from which only app.yaml, main.py and requirements.txt are the most important files.

.
├── LICENSE
├── Makefile
├── README.md
├── app.yaml
├── main.py
└── requirements.txt

The file app.yaml is the deployment descriptor and contains a single line, the runtime:

<span style="font-weight:bold">runtime</span>:<span style="color:#bbb"> </span>python37<span style="color:#bbb">
</span>

The requirements.txt file contains the dependencies for the application. GAE will automatically download and install
the dependencies for use:

flask-basicauth
flask

The main application is defined in main.py which will be examined by GAE in order to deploy the web application:

<span style="color:#998;font-style:italic"># [START gae_python37_app]</span>
<span style="font-weight:bold">from</span> <span style="color:#555">flask</span> <span style="font-weight:bold">import</span> Flask, jsonify
<span style="font-weight:bold">from</span> <span style="color:#555">flask_basicauth</span> <span style="font-weight:bold">import</span> BasicAuth

app <span style="font-weight:bold">=</span> Flask(__name__)

app<span style="font-weight:bold">.</span>config[<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">BASIC_AUTH_USERNAME</span><span style="color:#b84">'</span>] <span style="font-weight:bold">=</span> <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">user</span><span style="color:#b84">'</span>
app<span style="font-weight:bold">.</span>config[<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">BASIC_AUTH_PASSWORD</span><span style="color:#b84">'</span>] <span style="font-weight:bold">=</span> <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">password</span><span style="color:#b84">'</span>
app<span style="font-weight:bold">.</span>config[<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">BASIC_AUTH_FORCE</span><span style="color:#b84">'</span>] <span style="font-weight:bold">=</span> True
basic_auth <span style="font-weight:bold">=</span> BasicAuth(app)

actors <span style="font-weight:bold">=</span> [
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">1</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">William Shatner</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">James T</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">2</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Leonard Nimoy</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Spock</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">3</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">DeForest Kelley</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Leonard McCoy</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">4</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">James Doohan</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Montgomery Scott</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">5</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">George Takei</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Hikaru Sulu</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">6</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Walter Koenig</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Pavel Chekov</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">7</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Nichelle Nichols</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Nyota Uhura</span><span style="color:#b84">'</span>},
    {<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">id</span><span style="color:#b84">'</span>: <span style="color:#099">8</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">actor</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Majel Barrett</span><span style="color:#b84">'</span>, <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">role</span><span style="color:#b84">'</span>: <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Christine Chapel</span><span style="color:#b84">'</span>}
]

@app.route(<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">/</span><span style="color:#b84">'</span>)
<span style="font-weight:bold">def</span> <span style="color:#900;font-weight:bold">hello</span>():
    <span style="font-weight:bold">return</span> <span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">Hello World!</span><span style="color:#b84">'</span>


@app.route(<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">/actors</span><span style="color:#b84">'</span>, methods<span style="font-weight:bold">=</span>[<span style="color:#b84"></span><span style="color:#b84">'</span><span style="color:#b84">GET</span><span style="color:#b84">'</span>])
<span style="font-weight:bold">def</span> <span style="color:#900;font-weight:bold">get_persons</span>():
    <span style="font-weight:bold">return</span> jsonify(actors), <span style="color:#099">200</span>
<span style="color:#998;font-style:italic"># [END gae_python37_app]</span>

To deploy the example type make deploy and to invoke the application type make browse. To remove the application,
it must be disabled. A GAE can only be disabled by means of the web console. You have to login to console.cloud.google.com,
choose your project, navigate to GAE, go to settings and disable the application.

Conclusion

Because of the predefined architecture, managed infrastructure and developer focused workflow, serverless applications
are very easy to create in GAE. With a minimum of code you can create highly scalable web applications and web services.
The platform manages the life cycle of the applications, and upgrades. Logging and monitoring are provided by Google Stackdriver.
When compared to deploying the same solution in AWS Fargate,
a GAE application has a lot less configuration. Next time we’ll look at deploying a docker container on Google App Engine!

Share this article: Tweet this post / Post on LinkedIn