Getting started with Chalice

Let's try replicating the API endpoint we did for 311 in the previous chapter as a serverless application. For that, we'll use a framework called Chalice.

Chalice is a Python package for serverless applications on AWS, and is itself developed by Amazon. It can take care of an application, from its template all the way to deployment. It's also great for testing, as it emulates deployment with no fee, authentication, or even internet connection required.

Before we start working on our serverless application, let's ask Chalice to generate a template. In your terminal, type this:

chalice new-project

After this, type the name of the project: 311estimate. This will generate a new folder with a few files:

311estimate/
|
├── .chalice/
│   └── config.json
|
├── .gitignore
├── app.py
└── requirements.txt

Those files are all you need to write and deploy an endpoint; first, take a look at app.py. Inside, chalice invokes an app object—very similar to what FastAPI does (and other web frameworks). To continue the pattern of similarity with FastAPI, Chalice uses a decorator around the function with the GET method, defining the routing for the endpoint. The dummy function returns a dictionary, which will be converted to JSON under the hood—exactly like FastAPI would do.

Before we start adding our own code, let's try running this application locally:

$ chalice local
Serving on 127.0.0.1:8000

Now, we can pass a request in our browser or by using curl in the Terminal:

$ curl -X GET http://localhost:8000/
{"hello": "world"}

Clearly, Chalice is working—we're serving a dummy API as a serverless application. In the next section, let's override the code to make it serve actual models!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.110.97