DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Serverless Templates for AWS and Python
kreuzwerker
kreuzwerker

Posted on • Originally published at kreuzwerker.de

Serverless Templates for AWS and Python

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

# in serverless.yml
package:
  exclude:
    - node_modules/**
    - package.json
    - package-lock.json
Enter fullscreen mode Exit fullscreen mode

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

# in serverless.yml
plugins:
  - serverless-python-requirements
Enter fullscreen mode Exit fullscreen mode

The good thing about having it written in node, you don’t need to install it globally because npx 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 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.

plugins:
  - serverless-python-requirements
  - serverless-wsgi # This is needed when developing with Flask or Django
Enter fullscreen mode Exit fullscreen mode

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
Zappa Framework

Another alternative is 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 such as Flask, Django, or Gunicorn to AWS.

pip install zappa
zappa init
zappa deploy
Enter fullscreen mode Exit fullscreen mode

Eh, seriously? That’s all, yep!πŸ˜„

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

---
dev:
  app_function: app.app # Your handler code
  s3_bucket: aws_bucket_name # Where the Code will be deployed
Enter fullscreen mode Exit fullscreen mode

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

---
dev:
  django_settings: my_django_app.settings # For Django
  s3_bucket: aws_bucket_name
Enter fullscreen mode Exit fullscreen mode

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 logo jolo-dev / zappa-template

A template for Zappa powered by Cookiecutter

Zappa Template

This repository should ease up your project setup by using Cookiecutter for choosing Flask or Django and Zappa for deploying your AWS Lambda.

Pre-Requisite

Make sure you have following locally installed.

# Best to install it globally
python3 -m pip install cookiecutter
cookiecutter gh:jolo-dev/zappa-template
Enter fullscreen mode Exit fullscreen mode

Choose your Engine

  • Flask
  • Django

Development

Wanna improve this?

cd *project_slug*
python3 -m venv .env
source .env/bin/activate
pip install --upgrade pip # In case you need to
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Deployment

After setting up, you should be able to run following commands.

zappa deploy dev
Enter fullscreen mode Exit fullscreen mode
# For Changes and Updating
zappa update dev
Enter fullscreen mode Exit fullscreen mode
# Tear down the deployment
zappa undeploy dev
Enter fullscreen mode Exit fullscreen mode

Working with Django

Django is a fully fleshed application which requires a database In this template, the default database is deactivated Here is a very good instruction, how to setup a database…

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!

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: