Serverless Templates for AWS and Python
Tags: aws, cloud, engineering, serverless
Published: 2022-08-02 10:54:08

Written by John Nguyen.

## AWS Lambda

AWS Lambda is event-driven, and supposed to be the smallest __compute__ unit in the category of Compute in the AWS-universe. Most of the time when speaking of serverless, many people think about AWS Lambda. Lambda is the FaaS of AWS, which was introduced in 2014.

Currently, there are runtimes for Node.js, Python, Java, Go, and C#. If your favorite programming language is not on the list, you can create a custom runtime, e.g. for PHP or Rust.
Btw the Python runtime is the most popular one.

## Frameworks

Even if serverless abstracts the OS-management for you, it might be overwhelming in deploying a serverless function.

When working with AWS Lambda, the deployment process can be tedious and long. By using a framework, we have a standardized, simplified building process that agrees on common knowledge. Frameworks are mostly built by a large open-source community, which helps in finding common debugging tools and use cases.

![Blog Serverless Templates](//images.ctfassets.net/hqu2g0tau160/2u9oRJ2buHV5IBC2JV5sir/836f96646559d17dc3514ea1d0d89f92/Blog_Serverless_Templates.gif)
*Serverless Framework*

Don’t get confused by the name, but Serverless the actual name of the framework. It has 36k stars on Github (May 2020) and is the most popular serverless framework, and there are plenty of examples and plugins. You configure all the resources and functions within a serverless.yml and it will take care of dependencies, deployment to the cloud, and events.

However, the __Serverless__ Framework is written in NodeJS, which is an extra layer when developing with Python. That means you should remove all the node dependencies or its related files:

```NodeJS
# in serverless.yml
package:
  exclude:
    - node_modules/**
    - package.json
    - package-lock.json
```

As a Python developer, you probably want to install packages and have your requirements.txt ready. You definitely will require the [Serverless Requirements-Plugin](https://github.com/UnitedIncome/serverless-python-requirements "Serverless Requirements-Plugin")

```NodeJS
# in serverless.yml
plugins:
  - serverless-python-requirements
```

The good thing about having it written in node, you don’t need to install it globally because [npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b "medium blog") could handle it.

Phew… It's lot to take into consideration…

## Creating A Flask App

I hope your head is not spinning. Now you might think, *Wait, I have a [Flask](https://palletsprojects.com/p/flask/ "Flask") or my [Django](https://www.djangoproject.com/ "Django") app where the framework is taking care of the endpoints in my python code. Do I need to recreate several lambdas?* A valid question. Luckily, there is also an awesome [Plugin](https://github.com/logandk/serverless-wsgi "Github") for that.

```NodeJS
plugins:
  - serverless-python-requirements
  - serverless-wsgi # This is needed when developing with Flask or Django
```

## Using a Serverless Template

Stay with me, I got this. I have created a template that you can use for your Flask app.

https://github.com/jolo-dev/serverless-flask

By using that template, you have a ready-for-deployment configured serverless.yml- file 😉 In the future, I will create a similar one for Django.

![zappa](//images.ctfassets.net/hqu2g0tau160/3kRvOL6SHgiE7soUOhykFX/d58e28d7f495476651e708c63dc27493/zappa.jpeg)
*Zappa Framework*

Another alternative is [Zappa](https://github.com/Miserlou/Zappa "Zappa") which is built-in Python Serverless Framework and it serves only that runtime on AWS.

The cool thing is that you can easily migrate your [WSGI- application](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface "WSGI- application") such as Flask, Django, or [Gunicorn](https://github.com/benoitc/gunicorn "Gunicorn") to AWS.

```NodeJS
pip install zappa
zappa init
zappa deploy
```

Eh, seriously? That’s all, yep!😄

The configuration (default is JSON but I prefer YAML) is much smaller than that of Serverless Framework

```NodeJS
---
dev:
  app_function: app.app # Your handler code
  s3_bucket: aws_bucket_name # Where the Code will be deployed
```

3(!!) lines of code 😱 That’s really promising. If you have a Django-app then you need to point to its settings:

```NodeJS
---
dev:
  django_settings: my_django_app.settings # For Django
  s3_bucket: aws_bucket_name
```

If you want to get started, I created a template where you can choose Flask or Django as your engine. That template is powered by [Cookiecutter](https://cookiecutter.readthedocs.io/en/1.7.2/ "Cookiecutter") which is a pre-requisite for using my template.

{% github jolo-dev/zappa-template %}

However, the documentation is kind of difficult to read if you want to customize it.

## Comparison

Here is a comparison between Zappa and Serverless Framework.

|Serverless Framework|Zappa Framework| | — -| — -| |+ Multi-vendor|+/- Python Only| |+ Easy and Fast to setup|+ Super fast| |+ Many Plugins|+ Easy Migration of WSGI Applications| |+ Cloudformation in YAML|+/- AWS Only| |+ Support of Many Programming Languages|- Documentation is overwhelming (personally, I don’t like it)| |- Written in NodeJS and not Python-Only|- No Plugins| |- Many Configurations to make it Python-Ready|

## Conclusion

Clearly, as always, it depends on what your needs are. However, there is a new shift towards using serverless architecture and nowadays with a rich ecosystem and big community, there are many solutions in making serverless development frictionless.

Thanks for reading and Happy Coding!