← Back to blog

Serverless Templates for AWS and Python

· 4 min read
aws serverless python devops

Serverless development supposed to abstract underlying infrastructure and lets you focus on business logic. AWS Lambda revolutionized the way we build software today.

AWS Lambda

AWS Lambda is an event-driven, 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 or on a function. Lambda is the FaaS of AWS which was introduced in 2014.

Currently, there are runtimes for Node.js, Python which is btw the most popular one, 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.

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.

Serverless Framework

The Serverless Framework

Don't get confused by the name but it's the actual name of the framework. It is with 36k stars on Github (May 2020) the most popular serverless framework and there a plenty of examples and plugins. You configure all the resources and functions within a serverless.yml and it will take 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:

yaml

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

As a Python developer, you probably want to install packages and you have your requirements.txt ready. You definitely will require the Serverless Requirements-Plugin

yaml

# 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 could handle it.

Puh... it 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 or my 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 for that.

yaml
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.

{% github jolo-dev/serverless-flask %}

bash

# sls works as well
npx serverless install --url https://github.com/jolo-dev/serverless-flask --name my-flask-app

cd my-flask-app && npm run setup

# answer prompt

# Ready for deployment
npx serverless deploy

# Don't forget to remove the deployment
npx sls remove

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 Framework

Zappa

Another alternative is Zappa which is built-in Python Serverless Framework and it's serving only that runtime on AWS.

The cool thing is that you can easily migrate your WSGI- application such as Flask, Django, or Gunicorn to AWS.

bash
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 of Serverless Framework

yaml
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 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

Even though it looks so less in order to use Zappa, I made a comparison.

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 you're needs are. However, there is a new shift towards Serverless Architecture and there are an ecosystem and many solutions in making serverless development frictionless. For sure there is a solution for you. Thanks for readying and Happy Coding!