aws-lambda-cost-blog

opentracing-compatibility-for-go-agent

introduction-to-aws-chalice

 

introduction-to-aws-chalice

Serverless is still an unexplored realm for many developers and the idea of transitioning from traditional servers to serverless may look like a daunting task. To ease this shift many communities have come up with some really good resources, whether it be articles or frameworks. In this blog, I will be introducing one such microframework for Python, developed by engineers at AWS, called Chalice. I will not go into a lot of details in this blog but highlight important parts of the microframework.

Getting Started

AWS Chalice, is a Python microframework for serverless development using AWS Lambda. It takes only a few minutes to set up and deploy your working serverless applications with Chalice. It takes care of most of the configuration that you would need to do for deploying a serverless application by its own, thus, reducing the friction for new developers from entering serverless. Moreover, Chalice shares a lot of similarity with Flask web microframework in terms of code structure, making it easier for developers who are familiar with Flask to adopt Python serverless development.

Chalice Sample Project

If you are going to try out the upcoming code snippets on your local, it is advisable to set up a virtual environment and install Chalice there:
 

    virtualenv chalice-demo-venv 
    source chalice-demo-venv/bin/activate
    pip install chalice


Now that chalice is installed we can simply create an app using the `new-project` command:

    chalice new-project <project-name> 

It creates a new directory with the bare minimum files necessary to deploy and get your app up and running. Navigate into your project directory and deploy your project to AWS.

    cd <project-name>
    chalice deploy


Note
: Make sure you have set up your AWS credentials usually located at ~/.aws/config before deploying.

You will see that all the required resources are automatically created in your AWS account.

    Creating deployment package.
    Creating IAM role: project-name-dev
    Creating lambda function: project-name-dev
    Creating Rest API
    Resources deployed:
        -Lambda ARN: arn:aws:lambda:eu-west-2:111111111111:function:project-name
        -Rest API URL: https://xxxxxxxxxx.execute-api.eu-west-2.amazonaws.com/api/

Try making a request to the API:

    curl -X GET https://xxxxxxxxxx.execute-api.eu-west-2.amazonaws.com/api/


You should be seeing the following as the response:
 

    {"hello":"world"}


Now if we look our app.py we see the simple function

    from chalice import Chalice
app = Chalice(app_name='new-name')
@app.route('/' def index(): return {'hello': 'world'}


Congratulations! You have successfully set up and run your first Chalice app. You can see how simple it was to set up and deploy your Lambda function. Chalice handles the IAM role generation with exactly the required permission policies. Our API call triggered index() function because it was set to be invoked whenever the request is made with an empty routing path (‘/’).

Working with Chalice

In the previous section, our API call triggered `index()` function because it was set to be invoked whenever the request is made with an empty routing path (‘/’). Similarly, we can set different Lambda functions to be invoked with a different routing path. For instance, let’s introduce another function as follows in our `app.py`:

    @app.route('/key')
    def key_func():
          return {'key': 'invoked'}

And deploy the project again with `chalice deploy`. Now make the following request:

    curl -X GET https://xxxxxxxxxx.execute-api.eu-west-2.amazonaws.com/api/key

This would trigger `key_func()` function instead and return the appropriate return value.

You can also pass parameters via URL. The following example demonstrates how to do that:

    @app.route('/key/{value}')
    def key_func(value):
        return {'key': 'value'}


In the above function `value` is passed from the URL. The variable name in the URL and function argument must be the same. Moreover, we can also configure our function to only be invoked for specific HTTP methods by passing the list of methods in the `methods` argument of the decorator. The following method will only be invoked if the request to the specified routing path is made with the “POST” or “PUT” method:

    @app.route('/key/{value}', methods=['POST', 'PUT'])
    def key_func(value):
        return {'key': 'value'}


Chalice also supports integrations with Amazon SQS, Amazon SNS, Amazon S3, and Amazon CloudWatch Event. You can set triggers for your Lambda functions to be invoked by any of the aforementioned services. 

Chalice is also developer friendly. It supports local development allowing developers to develop and test their functions on local before publishing it to AWS. You can start running local utility:
 

    chalice local


Chalice will start running on port 8000 by default. Now you can make requests to localhost:
 

    curl -X GET http://localhost:8000/


This would invoke Lambda with empty routing path (‘/’). Similarly, you can invoke other functions by adding different routing paths to the request mimicking AWS behavior.

There are many more capabilities provided by the framework to give you control over your function. You can peruse through the documentation to fully explore Chalice.

Conclusion

AWS Chalice is an easy-to-use microframework, especially if you have prior experience with Flask. To summarize, and as its documentation also states, it provides:

  • A command-line tool for creating, deploying and managing your application.

  • A decorator based API for integrating with AWS services.

  • Automatic IAM policy generation.

Even though Chalice is not a comprehensive framework in terms of feature set, it encompasses major functionalities for most of the applications. By not including a few features, its usability is significantly simplified. AWS Chalice’s feature set would suffice for most developers but if anyone needs more functionalities, there are other more complex frameworks at their disposal.

KEEP READING

Chaos-test-your-lambda-functions-with-thundra

making-appsync-easier-with-thundra


Lambda Sources and Thundra Layers ~ A match made in Serverless

making-appsync-easier-with-thundra

KEEP READING

UI-version

a-note-from-the-board-v3

aws-step-functions

                                    aws-step-functions-v2

KEEP READING

monitoring-.NET-Lambda-Functions-with-Thundra

                                    monitoring-.NET-Lambda-Functions-with-Thundra

KEEP READING

serverless-observability-fundamentals