Deploy Flask Applications as AWS Lambda with Zappa

Sogo Ogundowole
Hacktive Devs
Published in
5 min readAug 1, 2022
Source: Zappa

AWS Lambda falls in the category of serverless computing on AWS. The term serverless is sometimes misconstrued as architecture without a server, this is not right.

According to AWS,

Serverless computing allows you to build and run applications and services without thinking about servers. Serverless technologies feature automatic scaling, built-in high availability, and a pay-for-use billing model to increase agility and optimize costs.

Some of the serverless services on AWS are —

More details can be found here about AWS serverless services.

AWS Lambda

One of the best use cases and advantages of AWS Lambda is the pay-for-use billing approach. This implies one only gets billed on the up/usage time of the application. Use cases of Lambda involve scenarios where:

  • Traffic or usage for service/application is not high
  • The application is not actually required to be “live” always.
  • It is a scheduled job
  • It is an event-driven application

Zappa

Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as “serverless” web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance — and at a fraction of the cost of your current deployments!

Zappa creates the ease of deploying applications as lambda apps via a simple config that handles every other thing under the hood. It supports Python web apps (including — but not limited to Django, Flask, and Bottle apps).

Zappa was formerly managed on https://github.com/Miserlou/Zappa but was later moved to https://github.com/zappa/Zappa.

Requirements

  1. Python 3.6 - 3.9

Zappa's default requirements on the version being used might break your build if your requirements conflict with Zappa’s. This sometimes occurs with setuptools and troposphere

To solve this the --force-reinstall argument with pip to override the requirements that conflict

This is rare, but overriding requirements usually clear out this potential break.

2. AWS Credentials

Ensure your AWS credentials have the right permissions and policies for lambda and S3 buckets. Also, ensure these credentials are rightly set in these file locations on your computer:

And the content should look like this:

Here is a detailed document on how to get your secret and access key on AWS.

Setup

In this article, we will deploy a simple flask application that returns “My Lambda App works”

  • Create and activate the virtual environment. This is important because without the virtual environment Zappa would not be able to deploy successfully.
$ python -m venv venv
$ source venv/bin/activate
  • Install of requirements
$ pip install zappa flask
  • Create the app.py
Source: app.py

To initialize Zappa, run:

$ zappa init

This prompt shows up to select stages: dev, staging and production. Default is dev

The next prompt is to select what AWS creds to be used:

The next prompt is to specify the name of the S3 bucket

The next prompt is to specify the app’s function

The last prompt is to specify the region, this could either be globally or a specific region:

Before the Zappa config is created you can preview the settings:

The zappa_settings.json will be auto-created and the file structure will look like this:

Note that without running $ zappa init you can also create yours zappa_settings.json manually. More configs AWS Lambda (i.e VPC, timeouts, schedule, size) can be found on Zappa’s README page.

Deployment

Run

$ zappa deploy [stage]

Since the stage initialized is dev , the command will be:

$ zappa deploy dev

This will run for some minutes, once deployed the base URL to this application will be logged to the console:

To test this deployment, Postman (or any other tool for HTTP requests) can be used:

To update an existing/already deployed app:

$ zappa update [stage]

Lambda Console

On the lambda console, more configurations can be done for VPCs, Size, Tags, Timeout, Environment variables etc can be done. The logs for each request are auto-logged to Cloudwatch.

AWS Lambda console

Conclusion

Zappa’s main purpose is to abstract the core technicalities involved in deploying already existing web apps in python and it does it pretty well. Its usage, as stated earlier, is not limited to just Flask, as other python web frameworks can also be deployed as lambda via Zappa.

If you find this article helpful, kindly give some claps and follow. Thanks!

--

--