Chalice

Table of contents
  1. What is Chalice?
  2. Install Chalice
  3. Create a new project
  4. Deploy / Delete
  5. Multifiles
  6. Configuration File
    1. Environment Variables
  7. Deploying with Terraform

What is Chalice?

It is a python serverless microframework.

What it essentially does is combine AWS API Gateway and associated Lambda functions to help you quickly deploy a microservice.

Everything you can do in Chalice you can do in the AWS console, but it is easier to manage via code.

The syntax and the concept is very much similar to Flask if you’re familiar with it.


Install Chalice

pip3 install chalice

As of now (2021-05), chalice best supports Python 3.8.
As of now (2022) chalice supports Python 3.9.


Create a new project

To create a new project,

chalice new-project myproj

This will create a myproj directory

myproj
├── .chalice
├── app.py
└── requirements.txt

Deploy / Delete

The AWS credentials must already be set in ~/.aws/config.

To deploy, simply

chalice deploy
chalice deploy --stage ${stage} --profile ${profile}

To delete,

chalice delete
chalice delete --stage ${stage} --profile ${profile}

Multifiles

If you want to have multiple .py files apart from the app.py(which you will), place all the lib or utils related file in a folder called chalicelib.

Anything you add to this directory is recursively added to the deployment.

myproj
├── .chalice
├── app.py
├── chalicelib
└── requirements.txt

Configuration File

In .chalice, there is a file called config.json.

This folder contains all the configurations related to this package. You can set app name, deploment stages, environment variables, etc.

Environment Variables

For general environment variables, add the following syntax to .chalice/config.json

{
  "environment_variables": {
    "ENV_VAR": "value",
    "ENV_VAR2": "value2"
  }
}

You can also set stage specific environment variables by,

{
  "stages": {
    "dev": {
      "environment_variables": {
        "MY_ENV": "value"
      }
    },
    "prod": {
      "environment_variables": {
        "MY_PROD_ENV": "value"
      }
    }
  }
}

Deploying with Terraform

# Will generate deployment.zip and chalice.tf.json
chalice package --pkg-format terraform output_dir

chalice package will generate the Lambda deployments and Terraform configuration files. You can then use Terraform CLI to deploy.

See here for details.


References: