AWS Chalice

Understanding the usage of AWS Chalice with LocalStack

AWS Chalice is a serverless micro framework used to develop and deploy your serverless applications on AWS resources. Chalice provides integrated functionality with most of the AWS Toolings like S3 Storage, Simple Queue Service, API Gateway and more. It offers a handy CLI interface that allows you to easily create, develop & deploy your serverless applications.

LocalStack offers an AWS Chalice client that allows you to interact with your Chalice applications locally. Using LocalStack, you can kick-start your development process, create a new Chalice application, and test it application locally.

Creating a new Chalice project

Start LocalStack inside a Docker container by running:

  1. $ localstack start -d

Install the chalice-local package by running:

  1. $ pip install chalice-local

You can now create a new Chalice project by running:

  1. $ chalice-local new-project

You will be prompted with an interactive menu where you can choose the name of your project and the project type. In this example, we are using localstack-test as the project name and REST API as the project type:

  1. ___ _ _ _ _ ___ ___ ___
  2. / __|| || | /_\ | | |_ _|/ __|| __|
  3. | (__ | __ | / _ \ | |__ | || (__ | _|
  4. \___||_||_|/_/ \_\|____||___|\___||___|
  5. The python serverless microframework for AWS allows
  6. you to quickly create and deploy applications using
  7. Amazon API Gateway and AWS Lambda.
  8. Please enter the project name
  9. [?] Enter the project name: localstack-test
  10. [?] Select your project type: REST API
  11. > REST API
  12. S3 Event Handler
  13. Lambda Functions only
  14. Legacy REST API Template
  15. [CDK] Rest API with a DynamoDB table
  16. Your project has been generated in ./localstack-test

Let’s take a look inside the project structure:

  1. tree
  2. .
  3. ├── app.py
  4. ├── chalicelib
  5. └── __init__.py
  6. ├── requirements-dev.txt
  7. ├── requirements.txt
  8. └── tests
  9. ├── __init__.py
  10. └── test_app.py
  11. 2 directories, 6 files

The app.py is our main API file. It has only one Route that would assign the URL of the application to the function. The decorators here primarily “wrap” functions here which makes it easy to write Code Logic by breaking them down into separate routes. For now, our Application is serving only a JSON Message which is {'hello': 'world'}.

Testing the Chalice API

Just as with AWS, you can now test your API using chalice-local local:

  1. $ chalice-local local
  2. Serving on http://127.0.0.1:8000

You can also do a curl to test the API:

  1. $ curl -X GET http://127.0.0.1:8000
  2. {"hello":"world"}

Deploying the Chalice API

You can use chalice-local deploy to deploy the REST API now:

  1. $ chalice-local deploy
  2. Creating deployment package.
  3. Creating IAM role: localstack-test-dev
  4. Creating lambda function: localstack-test-dev
  5. Creating Rest API
  6. Resources deployed:
  7. - Lambda ARN: arn:aws:lambda:us-east-1:000000000000:function:localstack-test-dev
  8. - Rest API URL: https://y5iuni004m.execute-api.us-east-1.amazonaws.com/api/

We now have our Chalice Application deployed on a Lambda Amazon Resource Name (ARN) along with a REST API URL.

Last modified May 31, 2022: add documentation for the Chalice Integration (#142) (b5fa3843)