Thanks to my peer Sharon Li for co-authoring this post. Note: this is not an official blog post from AWS, rather a pattern I am sharing with the community. All code is provided under the MIT license without liability.
It can be difficult to host a Streamlit app on AWS using serverless and secure it with a HTTPS endpoint (see discussions on Stackoverflow [1] [2] and Streamlit [3]). As of this writing (Dec 2023), AWS AppRunner or API Gateway HTTP or REST APIs do not yet support Streamlit, you will get a loading screen similar to below. Perhaps that is how you ended up here :)
In this blog post we will provide a way to easily deploy a Streamlit app on AWS using serverless technologies: AWS ECS Fargate frontended by an Application Load Balancer (ALB), which is accessed by a secure HTTPS URL from Amazon CloudFront. All of this is deployed using the AWS CDK using a single cdk deploy
command.
Pre-requisites:
- AWS CDK. See Install the AWS CDK
- AWS Account with access to create and administer the following resources: Amazon CloudFront, ECS Fargate, Application Load Balancer (ALB), VPC.
- Tools: Docker, Python3, Git, AWS CLI
Please configure the AWS CLI using IAM credentials or a profile. Then use steps below to clone the streamlit-serverless GitHub repo, set up a virtual environment, install requirements, and deploy a Streamlit example app to AWS using CDK. The CDK deployment process will use the Docker daemon on your local system to build the application container and publish to ECR.
# Install or update CDK
npm install -g aws-cdk
# Clone the repo
git clone https://github.com/kawsark/streamlit-serverless.git
cd streamlit-demo-app
# Create an activate a Python virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install requirements
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# Bootstrap CDK if you have not done so before
cdk bootstrap
# Run CDK deploy
cdk synth
cdk deploy
The cdk deploy
command will provide a listing of the resources to be deployed and provide the following prompt: “Do you wish to deploy these changes (y/n)?” Answer y
and hit enter to deploy the Stack. Upon successful deployment, the CDK will output the URL to access the deployed Streamlit app.
Please access the StreamlitURL output value which should be similar to the following https://<cloudfront-distribution-id>.cloudfront.net. When you access the URL, you should see a screen such as below.
If you press the lock icon next on your browser, you will notice that it is protected by a TLS certificate with Amazon as the trusted Certificate Authority (CA).
Deploying your own application
The repository encapsulates a starter application in the streamlit_sample
sub-directory with a corresponding Dockerfile. You can drop your own application + Docker file in this directory, or point to a different sub-directory from the file frontend_stack.py.
Cost
You can view the Resources created by this Stack on the AWS Console. Navigate to the CloudFormation dashboard, and choose the Stack called StreamlitServerlessApp-FrontendStack. Select the Resources Tab to view associated resources. The cost of this Stack will be dependent on usage. Please use the AWS Pricing calculator to project your costs for the resource types. An example is shown for the ECS task below.
The sample Streamlit app is hosted on a single ECS Fargate task with a minimum configuration of .25 vCPU and .5 GB memory. Lets say you use the app for 30 minutes a day and it uses the included 20 GB temporary storage in the us-east-1 region. Using the pricing calculator, we get $0.38 per month for ECS.
Note that this is just one component of the overall solution. Please add all services to the AWS cost calculator for a representative estimate. You can also navigate to the AWS Billing and Cost management dashboard to view your month-to-date cost, and setup any budgets using AWS Budgets.
Clean-up
To cleanup the resources, activate the python virtual environment and run the cdk destroy
command.
Conclusion
There you have it, a short a sweet post that describes how to use the CDK to host a Streamlit serverless App and access it using HTTPS.