Python Serverless Microframework for AWS (Chalice)

Python Serverless Microframework for AWS (Chalice)

If you’re into serverless stuff, you already know what is AWS Lambda. But many people use the aws web console to create the lambda functions and write the python code over there

Why Chalice?

  1. For creating the lambda functions

  2. Test in locally

  3. You can deploy into server from CLI

  4. You can create multiple stages like (TEST, PROD, DEV)

  5. You can add all event triggers like (on_s3_event, SNS, SQS)

Required Packages

python3.6 -m venv env
source env/bin/activate
pip install chalice

Now you have the all chalice CLI interface you can check by below command

chalice --help

Creating new project

chalice new_project <project name>

Created project structure like below

$ cd <project_name>
$ ls -la
drwxr-xr-x   .chalice
-rw-r--r--   app.py
-rw-r--r--   requirements.txt

To run locally

By default it will run on 8000

<project_name>$ chalice local --port <port number>

Deploy into AWS

$chalice deploy
...
Initiating first time deployment...
https://qxea58oupc.execute-api.us-west-2.amazonaws.com/api/

You now have an API up and running using API Gateway and Lambda:

$ curl https://qxea58oupc.execute-api.us-west-2.amazonaws.com/api/
{"hello": "world"}

you can add the all routes in the app.py like

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

@app.schedule(Rate(5, unit=Rate.MINUTES))
def periodic_task(event):
    return {"hello": "world"}

@app.on_s3_event(bucket='mybucket')
def s3_handler(event):
    print(event.bucket, event.key)

Thank you ..