DEV Community

Cover image for Serverless: Deploy to AWS with two easy commands - Come Zappa
An Rodriguez
An Rodriguez

Posted on • Updated on

Serverless: Deploy to AWS with two easy commands - Come Zappa

Alt Text

Deploy a Flask/Django application to a serverless environment with just commands and an Amazon Web Services account.

The requests are handled by the API Gateway and proxied to your application running on a Lambda environment.

From Zappa's website:

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!

Keep asking why? Apart from being ultra-cheap (almost always much cheaper than an EC2 or a Docker ECS or Kubernetes cluster), it is by default completely hands-free: takes care of everything and just runs your app in the cloud. But it's customizable so you can completely manage your infrastructure while using Zappa to be between your app and the API Gateway.

Supported frameworks

TL;DR: Flask, Django
Enter fullscreen mode Exit fullscreen mode

If you are into Python you're probably interested in this article. More so if you're into frameworks.

A good friend of mine once told me about frameworks:

It doesn't matter which one you pick. But pick ONE.
Enter fullscreen mode Exit fullscreen mode

Those were the days I was more junior and bold. I basically wrote my own framework.

If you don't know what framework is, wikipedia says that a framework

provides a standard way to build and deploy applications and is a universal, reusable software environment that provides particular functionality as part of a larger software platform to facilitate the development of software applications, products, and solutions

Back in the day, I was coding everything myself. Great learning experience, for sure.

But, if you really want to make it, it is a good strategy to stand in the shoulder of giants. Do not invent the wheel: improve on the car.

Anyway, this little intro is just to say that if you are into the two most popular Python frameworks (otherwise, just bear with me), Flask and/or Django, then, my friend, you are in for a treat.

Hello, Flask World

I'm more of a Flask kind of guy. Flask is

a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.

As the quote says, Flask makes it really easy to start a new application, and it's proven to scale and grow with you.

Flask has many plugins you can use to extend the framework.

Remember to be lazy and avoid coding as the plague -- in a good way. What I mean is that if you can find a plugin that adapts to your needs then it is almost most certainly better than coding it yourself. On the one hand, it is very difficult to think all the details that other dozens of develops haven't already thought and resolved. On the other hand, you can even only extend an existing plugin to better suit your needs and at the same time help the community grow.

It is my opinion that at this day and age lonely wolves are less likely to thrive than more socially sharing packs. We can all grow faster together since our collective experience and abilities enhance us. No matter who you are you can always benefit from the interaction.

Remember to set up your virtual environment and installing Flask (if don't know what I mean by virtual environments click here)

Flask's website shows how to do a Hello, World web app::

from flask import Flask, escape, request

app = Flask(__name__)

def hello():
    name = request.args.get("name", "Flask world")
    return f'Hello, {escape(name)}!'
Enter fullscreen mode Exit fullscreen mode

And then to execute in the shell

In Linux, in the shell:

$ env flask run
 * Serving Flask app "hello"
 * Running on (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

In Windows' CMD (have you tried WSL2? it's awesome), you can do:

(venv) C:\Users\an\example>set

(venv) C:\Users\an\example>flask run
 * Serving Flask app ""
 * 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 (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

Now open your browser to and check the Hello, Flask world.

Come Zappa

Install Zappa in your virtual environment.

Now the two promised commands:

zappa init
... answer a few questions ...

zappa deploy
Enter fullscreen mode Exit fullscreen mode

That's it! Hello, Serverless.

Next Steps

I'd like to write a follow-up article on the myriad of configuration options you have with Zappa:

  • Keeping the server warm (faster response)
  • CORS
  • Use Zappa while managing yourself the infrastructure
  • Using custom IAM
  • Much more ...

If you are interested in a next article, comment/like/clap/react to this article to let me know.

Happy Serverless!

Top comments (0)