Chalice is a microframework for writing serverless apps in python. It allows you to quickly create and deploy applications that use AWS Lambda. It provides:
- A command line tool for creating, deploying, and managing your app
- A decorator based API for integrating with Amazon API Gateway, Amazon S3, Amazon SNS, Amazon SQS, and other AWS services.
- Automatic IAM policy generation
You can create Rest APIs:
from chalice import Chalice
app = Chalice(app_name="helloworld")
@app.route("/")
def index():
return {"hello": "world"}
Tasks that run on a periodic basis:
from chalice import Chalice, Rate
app = Chalice(app_name="helloworld")
# Automatically runs every 5 minutes
@app.schedule(Rate(5, unit=Rate.MINUTES))
def periodic_task(event):
return {"hello": "world"}
You can connect a lambda function to an S3 event:
from chalice import Chalice
app = Chalice(app_name="helloworld")
# Whenver an object is uploaded to 'mybucket'
# this lambda function will be invoked.
@app.on_s3_event(bucket='mybucket')
def handler(event):
print("Object uploaded for bucket: %s, key: %s"
% (event.bucket, event.key))
As well as an SQS queue:
from chalice import Chalice
app = Chalice(app_name="helloworld")
# Invoke this lambda function whenever a message
# is sent to the ``my-queue-name`` SQS queue.
@app.on_sqs_message(queue='my-queue-name')
def handler(event):
for record in event:
print("Message body: %s" % record.body)
And several other AWS resources.
Once you've written your code, you just run chalice deploy
and Chalice takes care of deploying your app.
$ chalice deploy ... https://endpoint/dev $ curl https://endpoint/api {"hello": "world"}
Up and running in less than 30 seconds. Give this project a try and share your feedback with us here on Github.
The documentation is available on readthedocs.
In this tutorial, you'll use the chalice
command line utility
to create and deploy a basic REST API.
First, you'll need to install chalice
. Using a virtualenv
is recommended:
$ pip install virtualenv $ virtualenv ~/.virtualenvs/chalice-demo $ source ~/.virtualenvs/chalice-demo/bin/activate
Note: make sure you are using python2.7 or python3.6. The chalice
CLI
as well as the chalice
python package will support the versions of python
supported by AWS Lambda. Currently, AWS Lambda supports python2.7 and
python3.6, so that's what this project supports. You can ensure you're
creating a virtualenv with python3.6 by running:
# Double check you have python3.6 $ which python3.6 /usr/local/bin/python3.6 $ virtualenv --python $(which python3.6) ~/.virtualenvs/chalice-demo $ source ~/.virtualenvs/chalice-demo/bin/activate
Next, in your virtualenv, install chalice
:
$ pip install chalice
You can verify you have chalice installed by running:
$ chalice --help Usage: chalice [OPTIONS] COMMAND [ARGS]... ...
Before you can deploy an application, be sure you have credentials configured. If you have previously configured your machine to run boto3 (the AWS SDK for Python) or the AWS CLI then you can skip this section.
If this is your first time configuring credentials for AWS you can follow these steps to quickly get started:
$ mkdir ~/.aws $ cat >> ~/.aws/config [default] aws_access_key_id=YOUR_ACCESS_KEY_HERE aws_secret_access_key=YOUR_SECRET_ACCESS_KEY region=YOUR_REGION (such as us-west-2, us-west-1, etc)
If you want more information on all the supported methods for configuring credentials, see the boto3 docs.
The next thing we'll do is use the chalice
command to create a new
project:
$ chalice new-project helloworld
This will create a helloworld
directory. Cd into this
directory. You'll see several files have been created for you:
$ cd helloworld $ ls -la drwxr-xr-x .chalice -rw-r--r-- app.py -rw-r--r-- requirements.txt
You can ignore the .chalice
directory for now, the two main files
we'll focus on is app.py
and requirements.txt
.
Let's take a look at the app.py
file:
from chalice import Chalice
app = Chalice(app_name='helloworld')
@app.route('/')
def index():
return {'hello': 'world'}
The new-project
command created a sample app that defines a
single view, /
, that when called will return the JSON body
{"hello": "world"}
.