Shipping a web application usually involves having your code up and running on single or multiple servers. In this model, you end up setting up processes for monitoring, provisioning, and scaling your servers up or down. Although this seems to work well, having all the logistics around a web application handled in an automated manner reduces a lot of manual overhead. Enter Serverless.
With Serverless Architecture, you don’t manage servers. Instead, you only need to ship the code or the executable package to the platform that executes it. It’s not really serverless. The servers do exist, but the developer doesn’t need to worry about them.
AWS introduced Lambda Services, a platform that enables developers to simply have their code executed in a particular runtime environment. To make the platform easy to use, many communities have come up with some really good frameworks around it in order to make the serverless apps a working solution.
By the end of this tutorial, you’ll be able to:
- Discuss the benefits of a serverless architecture
- Explore Chalice, a Python serverless framework
- Build a full blown serverless app for a real world use case
- Deploy to Amazon Web Services (AWS) Lambda
- Compare Pure and Lambda functions
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Getting Started With AWS Chalice
Chalice, a Python Serverless Microframework developed by AWS, enables you to quickly spin up and deploy a working serverless app that scales up and down on its own as required using AWS Lambda.
Why Chalice?
For Python developers accustomed to the Flask web framework, Chalice should be a breeze in terms of building and shipping your first app. Highly inspired by Flask, Chalice keeps it pretty minimalist in terms of defining what the service should be like and finally making an executable package of the same.
Enough theory! Let’s start with a basic hello-world app and kick-start our serverless journey.
Project Setup
Before diving into Chalice, you’ll set up a working environment on your local machine, which will set you up for the rest of the tutorial.
First, create and activate a virtual environment and install Chalice:
$ python3 -m venv env $ source env/bin/activate (env)$ pip install chalice==1.33.0 Follow our comprehensive guide on the Pipenv packaging tool.
Note: Chalice comes with a user-friendly CLI that makes it easy to play around with your serverless app.
Now that you have Chalice installed on your virtual environment, let’s use the Chalice CLI to generate some boilerplate code:
(env)$ chalice new-project serverless-sms-service A new directory is created with that name:
<project-name>/ | ├── .chalice/ │ └── config.json | ├── .gitignore ├── app.py └── requirements.txt See how minimalist the Chalice codebase is. A .chalice directory, app.py, and requirements.txt is all that it requires to have a serverless app up and running. Let’s quickly run the app on our local machine.
Chalice CLI consists of really great utility functions allowing you to perform a number of operations from running locally to deploying in a Lambda environment.
Build and Run Locally
You can simulate the app by running it locally using the local utility of Chalice:
Read the full article at https://realpython.com/aws-chalice-serverless-python/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]