A Very Quick Look at AWS Chalice


The South Park Fishsticks joke kinda rings in my head here.

"Do you like Flask?  Do you like to put Flask in your mouth?"

Doesn't quite fit, but if you like Flask, and you've been wanting to get your hands dirty with AWS Lambda and API Gateway, here's your chance.  And it's just like you'd expect it to be.

I do want to point out that Chalice was not the first entry into the Flask and Flask-like AWS deployment tool department.  A failure to mention Zappa (https://github.com/Miserlou/Zappa) as the first player into this space would be a disservice to the folks who built an excellent tool to deploy fully working Flask apps (and WSGI apps in general) to Lambda -- and without the dependency on AWS that Chalice is going to offer.  But Zappa is a complex tool, worth some serious consideration in this blog, and will be getting to it late this Spring.

But for now, Chalice.  It looks like a Flask application, but deploys as easily as a Gordon app.  That's the huge selling point.  If you've wired together an application manually through API Gateway to Lambda, you know just how much trouble it is to do simple things, like HTTP status codes and sometimes even GET requests.  And dropping in the need to do headers, can completely change how you do things in your lambda for the rest of your request data.  Even though you love the power of directly gluing together your API, you miss the simplicity of doing things in Flask.  Chalice scratches that Flask itch, while making deployments drop dead easy -- a complaint that has long plagued AWS.  What's not to like?  (We'll get to that later :) ).

Let's look at some code.


from chalice import Chalice, BadRequestError, ConflictError, Response


app = Chalice(app_name='hola')

STORE = {}


@app.route('/')
def index():
    return {'greeting': 'hola, bitches!'}

@app.route("/account/user", methods=['POST'])
def user_create():
    request = app.current_request.json_body
    if ("username" in request) and ("password" in request):
        username = request["username"]
        password = request["password"]
    else:
        raise BadRequestError("missing username and/or password")
    if username in STORE:
        raise ConflictError("username already taken")
    STORE[username] = password
    return Response(body=None, 

                    status_code=201,
                    headers={"Content-Type": "application/json"})

@app.route("/account/user/list", methods=["GET"])
def user_list():
    return STORE



A simple application.  No complications, like a database:  just a place to store a unique name and an unencrypted password.  It also sends a warm greeting in JSON, if you just hit the base URL (I like my APIs to be polite to window shoppers).

Let's deploy:




That's all it is to it.  A single command deploys your entire application to the default dev environment.

Let's see how well it works.





Looks like we got our friendly greeting.  Let's see how well it works, and add some data.



We got our 201, so it looks like it created some data.   We'll add some more quickly, and then see how well it does listing the data.




























Looks pretty good.  Even traps the exceptions and throws back the proper HTTP status.  If you've ever had to worry about that with normal Lambda development, you know what a pain that is.

It looks like a solid tool, and at the time of this writing, AWS has released 0.81, and calling it 'stable. The downside of Chalice (as opposed to Zappa) is that you buy into AWS.  But if your project is committed to using AWS, that kind of portability is probably not an issue for you.

Good luck with your project.

Peace
-r


References


Chalice - https://github.com/awslabs/chalice
Chalice documentation - http://chalice.readthedocs.io/en/latest/
Zappa - https://github.com/Miserlou/Zappa