My First Serverless App – ZAPPA + Flask + Lambda + Python

Introduction to Server-Less

Serverless computing is a method of providing backend services on an as-used basis. A serverless provider allows users to write and deploy code without the hassle of worrying about the underlying infrastructure.

A company that gets backend services from a serverless vendor is charged based on their computation and does not have to reserve and pay for a fixed amount of bandwidth or number of servers, as the service is auto-scaling. Note that despite the name serverless, physical servers are still used but developers do not need to be aware of them.

Introduction to Zappa

Zappa is the way to create and deploy the serverless web application, Zappa is Server-less Python Web Services for AWS Lambda and API Gateway.

Zappa makes it super easy to build and deploy the server-less application in the AWS Lambda, Zappa event driven Python application on AWS Lambda and API Gateway. This will bring the infinite scaling , zero downtime and zero maintenance.

Let’s start with writing a hello world program and setup the environment to test it.

IDE for your programming

I am very much comfortable with the Jetbrains product, I used their PhpStorm, Intellij, Pycharm, Team City, YouTrack and Upsource.

I purchase the ultimate edition of the Intellij for Java development and I have Python plugin installed in IntelliJ but for this post, I prefer to use Pycharm Community

My Development environment is Windows 10 with WSL2 installed and Docker running for all my databases, MessageQueuing Servers

Install a venv in Pycharm

Create a new project and setup the new environment using VirtualEnv, you can use your preferred virtual environment.

Install Zappa and Flask

Before we can deploy our web app on AWS Lambda with Zappa, first we need to install Zappa and a web framework to build our web app with. In this project, we are going to build a Flask app, so Flask needs to be installed too. You can install both of these packages with pip, the Python package manager.

(customer-repository) D:\Projects\customer-repository>pip install flask

Collecting flask
  Downloading Flask-2.0.0-py3-none-any.whl (93 kB)
     |████████████████████████████████| 93 kB 1.3 MB/s
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.0-py3-none-any.whl (288 kB)
     |████████████████████████████████| 288 kB 6.4 MB/s
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.0-py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 6.8 MB/s
Collecting click>=7.1.2
  Downloading click-8.0.0-py3-none-any.whl (96 kB)
     |████████████████████████████████| 96 kB 6.4 MB/s
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.0.0-py3-none-any.whl (18 kB)
(customer-repository) D:\Projects\customer-repository>pip install zappa
Collecting zappa
  Downloading zappa-0.52.0-py3-none-any.whl (114 kB)
     |████████████████████████████████| 114 kB 1.7 MB/s

This will install all the dependencies which need to deploy your application to AWS, which includes boto3, botocore, s3Transfer

Verify the Installation of Flask and Zappa

If you are getting any error related to the Werkzeug, uninstall the version and reinstall the 2.0.0

zappa 0.52.0 requires Werkzeug<1.0, but you have werkzeug 2.0.0 which is incompatible.
(customer-repository) D:\Projects\customer-repository>flask --help
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

Usage: flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.
(customer-repository) D:\Projects\customer-repository>zappa --help
usage: zappa [-h] [-v] [--color {auto,never,always}] {certify,deploy,init,package,template,invoke,manage,rollback,schedule,status,tail,undeploy,unschedule,update,shell} ...

Zappa - Deploy Python applications to AWS Lambda and API Gateway.

Simple Flask App

Preparing the environment is done, next will jump into the Application creation, which will build a REST API using Flask

Create a app.py with the below content

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return {'user': 'asvignesh'}


# We only need this for local development.
if __name__ == '__main__':
    app.run()

Then run the flask run command to run the application in local

(customer-repository) D:\Projects\customer-repository>flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Verify the API in Browser or Postman

Now the hello world application is ready and working in our local environment, which we will deploy in the AWS Lambda using the Zappa

Preparing the Environment for Deploy the code

Login to AWS Console and open the IAM service for generating the access key and secret access key for the user with the privilege to deploy and create services in AWS

Instead of creating user and assigning the policies, i prefer to create a group so that i can add / remove the user to the group to add the permission to the individual users

Here is the IAM Policy which needed to deploy the service seamlessly

As I am using the SSO ( Single Sing On ) for the accounts I manage, I added the permission to my username and generated the temporary session token and put that in credentials file, you can generate the access key and secret access key for the user and put it in the config file

Create Zappa settings file from Scratch

(venv) D:\Projects\customer-repository>zappa init

███████╗ █████╗ ██████╗ ██████╗  █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
  ███╔╝ ███████║██████╔╝██████╔╝███████║
 ███╔╝  ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║  ██║██║     ██║     ██║  ██║
╚══════╝╚═╝  ╚═╝╚═╝     ╚═╝     ╚═╝  ╚═╝

Welcome to Zappa!

Zappa is a system for running server-less Python web applications on AWS Lambda and AWS API Gateway.
This `init` command will help you create and configure your new Zappa deployment.
Let's get started!

Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
What do you want to call this environment (default 'dev'):

I went ahead with the default settings and modify the zappa_Settings.json file according to my environment

Deploy the application


(venv) D:\Projects\customer-repository>zappa deploy --all
(Werkzeug 2.0.0 (d:\projects\customer-repository\venv\lib\site-packages), Requirement.parse('Werkzeug<1.0'), {'zappa'})
Calling deploy for stage dev..
Creating customer-reposi-dev-ZappaLambdaExecutionRole IAM Role..
Creating zappa-permissions policy on customer-reposi-dev-ZappaLambdaExecutionRole IAM Role.
Downloading and installing dependencies..
 - pyyaml==5.4.1: Downloading

The above command will download the dependencies and upload to S3 bucket and deploy in the Lambda then it will create the API Gateway Endpoints and required IAM roles automatically to run the application

Run the Application

Once the deployment is successful you will see the API in the build log, call that API you will get the response

Hurrah! My First Server-Less application is deployed in AWS Successfully


Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading