/serverless

Learning about serverless options for Python like AWS Lambda

Primary LanguageJupyter NotebookMIT LicenseMIT

serverless 🚀

Learning about serverless options for Python like AWS Lambda

What is serverless for?

For short running tasks, like a stateless web service (often reffered to as RESTful API) a call to the serverless function will spin up a machine, perform the task and turn off the machine. The task might be receiving data through the call to a simple public URL (also called the API Gateway) or fetch data from a bucket. It would then compute your code. And it will save or return the results before shutting down the machine. As such, you don't need to manage the server running your code in serverless mode.

What it is not

For serving web assets, consider a content delivery network (CDN). Amazon CloudFront is an AWS service built specifically for this purpose. Her is a guide on how to use it with S3 and other options: Using various origins with CloudFront distributions

Chalice

repo: aws/chalice - website: aws.github.io/chalice/


First create a virtual environment

python -m venv env
.\env\Scripts\activate

Once activated, install Chalice

(env) pip install -r requirements.txt

Then create a new project:

chalice new-project helloworld

Move to the created project folder:

cd helloworld
code app.py

Create a simple REST API:

from chalice import Chalice

app = Chalice(app_name="helloworld")

@app.route("/")
def index():
    return {"hello": "world"}

Simply deploy the lambda function:

chalice deploy

Delete the function once you stop playing:

chalice delete

Tuto video

streamlit-streamlit_app-2022-11-14-22-11-15.webm

Call REST API from Postman image

Finally the Lambda function can be managed from the AWS console: image

Deploy serverless container with Fargate

https://github.com/slevin48/fargate

Resources