25/02/2025

Deploying Shiny for Python on AWS: A practical guide

Shiny (but also other frameworks like Dash) provide a powerful tool for creating interactive dashboards. And although the framework-specific servers can be a quick ways to get your app live, using a cloud services like AWS provides scalability, flexibility, and cost efficiency, while also offering robust security features like fine-grained access control. In this blog we will go over the different steps to get your shiny app up and running! 

A general overview of the approach can be found in the image below. We will use Docker to create a container of our app, push this container to AWS ECR and then use AWS Fargate within ECS to run the application from this container.   

Like in previous blogs, we will use these blocks to go over the code and specific steps of how you can implement this for yourself. By the end of this blog, you can have your Shiny app up and running on AWS.

Figure 1: Architecture of Shiny app deployment on AWS

Develop and build your app locally

    In this blog, we will not go over the details of how you can build a Shiny for Python application. The gallery and tutorials of Posit on the main Shiny for Python website give wide variety of examples and possibilities. The playground feature of the main website is also a great way to get familiar with the options. For our first step, we build a containerized image of the app. This allows you to package the app with all its dependencies, ensuring that it runs consistently across different environments.

    Next we create a Docker image using a Dockerfile and docker build. Make sure to test the containerized app afterwards using the docker run command.

    Figure 2: Example of Dockerfile

    AWS ECR: shipping your container

    We will now push this working Docker image to AWS Elastic Container Registry (ECR). AWS ECR is a fully managed container registry service for storing, managing and deploying Docker container images.

    EC2: Application Load Balancer and Security Group

    We will be using a AWS Elastic Container Service (ECS) task to run our application. However, ECS tasks are dynamic by design. When tasks are launched, their IP addresses can change, and you may not always know these in advance. This can create difficulty for your end-users. To ensure smooth communication to our app, we will use two aspects of EC2: Security Groups and Load Balancing.

    Security Groups control all inbound and outbound traffic on your app. The (Application) Load Balancer (or ALB) serves as the gateway for all traffic to our app. A major advantage of using a load balancer is that it provides a fixed url for the application, providing a consistent endpoint for the final users. The ALB will also can distribute requests over different tasks (making the app more scalable), filter traffic and perform health monitoring on your tasks.

    ECS Task Definition: the link to the container image

    In AWS Elastic Container Service (ECS), a Task Definition is a concept that defines how containerized applications are deployed and run. It serves as a blueprint for your application, specifying the containers to use (from for example ECR), the resources they need (CPU, memory, variables, …), and how they interact with one another. AWS currently allows you to choose between Fargate and EC2 as launch types. Fargate is serverless type in which AWS will manages the infrastructure, while EC2 requires more setup, but also provides more control over the underlying infrastructure.

    ECS Service and Tasks

    Within ECS a task is a running instance of a task definition, representing the actual containers performing the work. Tasks can be launched directly for ad-hoc needs or in our case be managed by a service. A service ensures high availability and scalability by maintaining the desired number of tasks running continuously on the ECS cluster. It monitors the tasks, automatically restarting any that fail, starts new tasks when the task definition is updated and can integrate with a load balancer to distribute traffic.

    Up and running: checking the Tasks and ALB

    Your app will now be running on an active task within your ECS cluster. Within this task, you can find the dynamic url. For the fixed url, check the load balancer. Unless you specially disabled it, you can also monitor the usage and logs of your app by visiting the AWS CloudWatch page. Happy dashboard building! 

    Written by

    Bjorn Rombouts

    Data Consultant @ Aivix