AWS Developer Blog

Chalice – 1.0.0 GA Release

by John Carlyle | on | in Python | Permalink | Comments |  Share

We’re excited to announce the 1.0.0 GA (Generally Available) release of Chalice!

Chalice is an open source serverless microframework that enables allows you to create and maintain application backends with a variety of AWS resources. These include:

Chalice 1.0.0 is now generally available and ready for production use. If you want, to give it a try you can download it from PyPi. You can install it with pip as follows.

pip install --upgrade chalice

We follow Semantic Versioning, and are dedicated to maintaining backwards compatibility for each major version.

Getting started with Chalice

You can get started with Chalice and deploy a fully functional API in just a few minutes by following our getting started guide.

You can find the complete documentation at readthedocs.

Notable Chalice features

Chalice provides many features to help build serverless applications on AWS. Here we provide an overview of a select few.

Building an API backend

The core of Chalice is the ability to annotate Python functions with a simple decorator that allows Chalice to deploy this function to AWS Lambda, and link it to a route in API Gateway. The following is a fully functional Chalice application, with a single linked route.

from chalice import Chalice

app = Chalice(app_name="helloworld")

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

This application can be deployed easily by running the command chalice deploy. Chalice takes care of all the machinery around bundling up the application code, deploying that code to Lambda, setting up an API Gateway Rest API with all the routes specified in your code, and linking up the Rest API to the Lambda function. Chalice will print out something like the following while it deploys the application.

Initial creation of lambda function.
Creating role
Creating deployment package.
Initiating first time deployment...
Deploying to: dev
https://.execute-api.us-west-2.amazonaws.com/api/

Once complete, you can send requests to endpoint it printed out at the end.

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

Dependency packaging

App packaging can be difficult in the Python world. Chalice will try to download or build all of your project requirements that are specified in a special requirements.txt file and add them to the code bundle that is uploaded to Lambda. Chalice will also try to build and deploy dependencies that have C extensions.

Pure Lambda functions

Pure Lambda functions enable you to deploy functions that are not tied to API Gateway. This is useful if you want to take advantage of the Chalice deployment and packaging features, but don’t need to call it over a REST API.

@app.lambda_function()
def custom_lambda_function(event, context):
    # Anything you want here.
    return {}

Scheduled events

Scheduled events let you mark a handler function to be called on some time interval using Amazon CloudWatch Events. It’s easy to add a scheduled job to be run using a Chalice scheduled event handler, as follows:

@app.schedule(Rate(12, unit=Rate.HOURS))
def handler(event):
    backup_logs()

Automatic policy generation

Automatic policy generation means that Chalice can scan your code for AWS calls, and generate an IAM policy with the minimal set of permissions your Lambda function needs to run. You can disable this feature and provide your own IAM policy if you need more control over exactly what permissions the Lambda function should have.

Authorizers

Chalice can handle a lot of common authorization workflows for you by providing hooks into both IAM authorization and Amazon Cognito user pools. If you want more control over your authorization methods, you can use a custom authorizer. This lets you call a Lambda function that runs custom code from your Chalice application to determine whether a request is authorized.

Continuous integration

You can use Chalice to build a continuous integration pipeline. This works by creating an AWS CodeCommit repository for your project code to live in, and an AWS CodePipeline that watches the repository for changes and kicks off a build in AWS CodeBuild whenever there are changes. You can configure the AWS CodeBuild stage to run tests on your code, and automatically deploy code to production if the tests all pass. The pipeline resources are all created and managed with an AWS CloudFormation template that Chalice will generate for you.

Upgrade notes

If you’re already a Chalice user, there are a few changes to be aware of when upgrading to version 1.0.0.

Parameters to route handling functions are now keyword arguments instead of positional arguments. In the following code, captured parts of the URI will be assigned to the argument with a matching name, rather than in the order they appear.

@route('/user/{first_name}/{last_name}')
def name_builder(last_name, first_name):
    return '%s %s' % (first_name, last_name)

This means that code in which the variable names don’t match the URI will now be broken. For example the following code will not work because the parameter and URI capture group don’t work.

@route('/user/{user_id}')
def get_user(user_number):
    return get_user(user_number)

Support for policy.json has been removed. It now must be suffixed with the stage name, for example, policy-dev.json.

Let us know what you think

We would love to hear your feedback. Feel free to leave comments or suggestions on our GitHub page.

Resources