12 Packaging and deploying Streamlit apps
This chapter explains how to move beyond beginner-friendly Streamlit Community Cloud deployments and prepare a Streamlit app for a more production-like environment. It highlights Community Cloud’s limitations, such as lack of custom domains, idle app shutdowns, resource constraints, and limited server customization, then compares alternatives ranging from managed platforms to raw cloud infrastructure. The chosen path is Docker plus AWS Elastic Beanstalk because it teaches widely useful deployment skills while balancing flexibility and manageability.
The chapter then walks through packaging a Streamlit app with Docker so it can run consistently across machines and cloud environments. It explains Dockerfiles, images, containers, port forwarding, volume mounts, and how to avoid embedding sensitive files like Streamlit secrets inside a Docker image by using a .dockerignore file. It also introduces Docker Compose as a cleaner way to define build instructions, port mappings, and mounted volumes, making local containerized development easier to repeat.
The final part focuses on deploying the Dockerized app to AWS Elastic Beanstalk, including creating an AWS account, configuring IAM permissions, preparing secrets for deployment, initializing the app with the EB CLI, creating an environment, and redeploying updates with a simple command. It also explains how Elastic Beanstalk uses services such as EC2, S3, load balancers, security groups, autoscaling, and monitoring behind the scenes. To make the app feel production-ready, the chapter finishes by showing how to attach a custom domain, request and validate an HTTPS certificate, and configure the load balancer so users can access the Streamlit app securely through a professional web address.
The Docker Desktop interface.
Docker runs your Streamlit app in a container.
Since we added secrets.toml to .dockerignore, we need another way to make it available to our app at runtime (see chapter_12/in_progress_01 in the GitHub repo for the full code).
The end state of our deployed Streamlit app on AWS.
The AWS Management Console.
Adding the appropriate permissions for your IAM user
Generating an access key and secret pair for your IAM user.
Our application and environment within the Elastic Beanstalk UI on the AWS console.
Adding a CNAME record to point your custom domain to your app.
Options to choose while requesting a certificate in ACM.
ACM shows CNAME records to add to your domain's DNS to prove ownership.
Adding an HTTPS listener and a certificate to your load balancer.
Your browser now confirms that HTTPS is enabled and the connection is secure.
Summary
- Streamlit Community Cloud, though free, comes with several limitations, such as resource limits, lack of support for a custom domain, and idle app shutdowns.
- There is a wide range of alternatives to choose from: low-complexity platforms like Render, medium-complexity options like Elastic Beanstalk, and even high-complexity ones like hand-configuring raw AWS services like EC2.
- Docker is a tool that makes it possible to package your app so it runs anywhere.
- To use Docker, you write a Dockerfile, build a Docker image from it, and spin up Docker containers from the image.
- A Dockerfile has instructions to build an image layer by layer.
- Streamlit offers configuration options that can be set using environment variables, a special config.toml file, or as flags in the streamlit run command.
- The docker build command builds an image, while docker run spins up a container.
- Docker Compose is a configuration manager for Docker, where you define the runtime instructions in a docker-compose.yml file.
- AWS is the world's leading cloud provider, offering services like EC2 for virtual machines and S3 for storage.
- Elastic Beanstalk is a layer on top of raw AWS services like EC2 and S3 that automatically configures these services for you, making it easy to deploy an app.
- You can use Identity and Access Management within AWS to set up various permissions for services like EC2 and Elastic Beanstalk to do various things.
- While preparing your Streamlit app for Elastic Beanstalk, you should specify the content of secrets.toml in a file within the .ebextensions folder rather than placing it along with your code.
- You can use the EB CLI to interact with Elastic Beanstalk in a terminal.
- The eb init commands sets up an EB application; eb create creates an environment under it.
- Use the eb deploy command to redeploy your app when you make changes.
- To add HTTPS to your EB app (without browser warnings), you need to get a custom domain from a domain registrar.
- AWS Certificate Manager will issue an SSL certificate for your domain once you verify ownership.
- To complete the process of enabling HTTPS, add a listener to your load balancer on port 443 and attach the issued certificate.
Build Python Web Apps with Streamlit ebook for free