Book Image

Building Serverless Python Web Services with Zappa

By : Abdulwahid Abdulhaque Barguzar
Book Image

Building Serverless Python Web Services with Zappa

By: Abdulwahid Abdulhaque Barguzar

Overview of this book

Serverless applications are becoming very popular these days, not just because they save developers the trouble of managing the servers, but also because they provide several other benefits such as cutting heavy costs and improving the overall performance of the application. This book will help you build serverless applications in a quick and efficient way. We begin with an introduction to AWS and the API gateway, the environment for serverless development, and Zappa. We then look at building, testing, and deploying apps in AWS with three different frameworks--Flask, Django, and Pyramid. Setting up a custom domain along with SSL certificates and configuring them with Zappa is also covered. A few advanced Zappa settings are also covered along with securing Zappa with AWS VPC. By the end of the book you will have mastered using three frameworks to build robust and cost-efficient serverless apps in Python.
Table of Contents (20 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

A minimal Flask application


Let's see what a minimal Flask application looks like:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
  return 'Hello World!'

That's it, we are done with the minimal Flask application. It's very simple to configure and create a microservice with Flask.

Let's discuss what exactly the preceding code is doing and how we would run the program:

  1. First, we imported a Flask class.
  2. Next, we created an instance of the Flask class. This instance will be our WSGI application. This first argument will be the name of the module or package. Here, we created a single module, hence we used __name__. This is needed so that Flask knows where to look for templates, static, and other directories.
  3. Then, we used app.route as a decorator with a URL name as a parameter. This will define and map the route with the specified function.
  4. The function will be invoked to the HTTP request with the URL specified in the route decorator.

To run this program, you can either use...