Text of realpython.com › aws-chalice-serverless-python as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Building Serverless Python Apps Using AWS Chalice – Real Python

Building Serverless Python Apps Using AWS Chalice by Saurabh Sharma Updated Oct 23, 2026 Reading time estimate 12m intermediate web-dev Table of Contents Remove ads Shipping a web application usually involves having your code up and running on single or multiple servers. In this model, you end up setting up processes for monitoring, provisioning, and scaling your servers up or down. Although this seems to work well, having all the logistics around a web application handled in an automated manner reduces a lot of manual overhead.

Enter Serverless. With Serverless Architecture , you don’t manage servers. Instead, you only need to ship the code or the executable package to the platform that executes it. It’s not really serverless. The servers do exist, but the developer doesn’t need to worry about them. AWS introduced Lambda Services , a platform that enables developers to simply have their code executed in a particular runtime environment.

To make the platform easy to use, many communities have come up with some really good frameworks around it in order to make the serverless apps a working solution. By the end of this tutorial, you’ll be able to : Discuss the benefits of a serverless architecture Explore Chalice, a Python serverless framework Build a full blown serverless app for a real world use case Deploy to Amazon Web Services (AWS) Lambda Compare Pure and Lambda functions Free Bonus: 5 Thoughts On Python Mastery , a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Getting Started With AWS Chalice Chalice , a Python Serverless Microframework developed by AWS, enables you to quickly spin up and deploy a working serverless app that scales up and down on its own as required using AWS Lambda. Remove ads Why Chalice? For Python developers accustomed to the Flask web framework, Chalice should be a breeze in terms of building and shipping your first app. Highly inspired by Flask, Chalice keeps it pretty minimalist in terms of defining what the service should be like and finally making an executable package of the same.

Enough theory! Let’s start with a basic hello-world app and kick-start our serverless journey. Project Setup Before diving into Chalice, you’ll set up a working environment on your local machine, which will set you up for the rest of the tutorial. First, create and activate a virtual environment and install Chalice: Language: Shell $ python3 -m venv env $ source env/bin/activate (env) $ pip install chalice == 1 .33.0 Follow our comprehensive guide on the Pipenv packaging tool .

Note: Chalice comes with a user-friendly CLI that makes it easy to play around with your serverless app. Now that you have Chalice installed on your virtual environment, let’s use the Chalice CLI to generate some boilerplate code: Language: Shell (env) $ chalice new-project serverless-sms-service A new directory is created with that name: <project-name>/ | ├── .chalice/ │ └── config.json | ├── .gitignore ├── app.py └── requirements.txt See how minimalist the Chalice codebase is.

A .chalice directory, app.py , and requirements.txt is all that it requires to have a serverless app up and running. Let’s quickly run the app on our local machine. Chalice CLI consists of really great utility functions allowing you to perform a number of operations from running locally to deploying in a Lambda environment. Build and Run Locally You can simulate the app by running it locally using the local utility of Chalice: Language: Shell (env) $ chalice local Serving on 127.0.0.1:8000 By default, Chalice runs on port 8000.

We can now check the index route by making a curl request to http://localhost:8000/ : Language: Shell $ curl -X GET http://localhost:8000/ {"hello": "world"} Now if we look at app.py , we can appreciate the simplicity with which Chalice allows you to build a serverless service. All the complex stuff is handled by the decorators : Language: Python from chalice import Chalice app = Chalice ( app_name = "serverless-sms-service" ) @app . route ( "/" ) def index (): return { "hello" : "world" } Note : We haven’t named our app hello-world , as we will build our SMS service on the same app.

Now, let’s move on to deploying our app on the AWS Lambda. Remove ads Deploy on AWS Lambda Chalice makes deploying your serverless app completely effortless. Using the deploy utility, you can simply instruct Chalice to deploy and create a Lambda function that can be accessible via a REST API. Before we begin deployment, we need to make sure we have our AWS credentials in place, usually located at ~/.aws/config . The contents of the file look as follows: Language: Config File [default] aws_access_key_id = <your-access-key-id> aws_secret_access_key = <your-secret-access-key> region = <your-region> With AWS credentials in place, let’s begin our deployment process with just a single command: Language: Shell (env) $ chalice deploy Creating deployment package.

Updating policy for IAM role: hello-world-dev Creating lambda function: hello-world-dev Creating Rest API Resources deployed: - Lambda ARN: arn:aws:lambda:ap-south-1:679337104153:function:hello-world-dev - Rest API URL: https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/ Note : The generated ARN and API URL in the above snippet will vary from user to user. Wow! Yes, it is really this easy to get your serverless app up and running.

To verify simply make a curl request on the generated Rest API URL: Language: Shell $ curl -X GET https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/ {"hello": "world"} Typically, this is all that you need to get your serverless app up and running. You can also go to your AWS console and see the Lambda function created under the Lambda service section. Each Lambda service has a unique REST API endpoint that can be consumed in any web application.

Next, you will begin building your Serverless SMS Sender service using Twilio as an SMS service provider. Building a Serverless SMS Sender Service With a basic hello-world app deployed, let’s move on to building a more real-world application that can be used along with everyday web apps. In this section, you’ll build a completely serverless SMS-sending app that can be plugged into any system and work as expected as long as the input parameters are correct.

In order to send SMS, we will be using Twilio , a developer-friendly SMS service. Before we begin using Twilio, we need to take care of a few prerequisites: Create an account and acquire ACCOUNT_SID and AUTH_TOKEN . Get a mobile phone number, which is available for free at Twilio for minor testing stuff. Install the twilio package in our virtual environment using pip install twilio==9.11.2 . With all the above prerequisites checked, you can start building your SMS service client using Twilio’s Python library.

Let’s begin by cloning the repository and creating a new feature branch: Language: Shell $ git clone <project-url> $ cd <project-dir> $ git checkout tags/1.0 -b twilio-support Now make the following changes to app.py to evolve it from a simple hello-world app to enable support for Twilio service too. First, let’s include all the import statements : Language: Python from os import environ as env # 3rd party imports from chalice import Chalice , Response from twilio.rest import Client from twilio.base.exceptions import TwilioRestException # Twilio Config ACCOUNT_SID = env . get ( "ACCOUNT_SID" ) AUTH_TOKEN = env . get ( "AUTH_TOKEN" ) FROM_NUMBER = env . get ( "FROM_NUMBER" ) TO_NUMBER = env . get ( "TO_NUMBER" ) Next, you’ll encapsulate the Twilio API and use it to send SMS: Language: Python app = Chalice ( app_name = "sms-shooter" ) # Create a Twilio client using account_sid and auth token tw_client = Client ( ACCOUNT_SID , AUTH_TOKEN ) @app . route ( "/service/sms/send" , methods = [ "POST" ]) def send_sms (): request_body = app . current_re

The rest of this page is on the live site.