DEV Community

JoLo
JoLo

Posted on

Serverless Templates for AWS and Python

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:

# 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 you 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.

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.

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.

GitHub logo jolo-dev / serverless-flask

Serverless Template for a Flask application

Serverless-Flask

The fastest way to a Flask application with Serverless.

Usage

Now per default (node >= 10.*), npx is pre-installed which makes the next line optional However, this is still recommended.

$ npm install -g serverless # Optional
$ (npx) serverless install --url https://github.com/jolo-dev/serverless-flask --name my-flask-app
$ cd my-flask-app && npm run setup
<answer prompts>
$ (npx) serverless deploy
Enter fullscreen mode Exit fullscreen mode

Once the deploy is complete, run sls info to get the endpoint:

$ sls info
Service Information
<snip>
endpoints:
  ANY - https://abc6defghi.execute-api.us-east-1.amazonaws.com/dev <-- Endpoint
  ANY - https://abc6defghi.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
Enter fullscreen mode Exit fullscreen mode

Copy paste into your browser, and voila!

Local development

To develop locally, create a virtual environment and install your dependencies:

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Then, run your app:

sls wsgi serve
 * Running on http://localhost:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
Enter fullscreen mode Exit fullscreen mode

Navigate to localhost:5000 to see…


# 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
Enter fullscreen mode Exit fullscreen mode

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.

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

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!

Top comments (0)