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.
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:
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
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
Using a Serverless Template
Stay with me, I got this. I have created a template that you can use for your Flask app.
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
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
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
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
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.
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.