In the previous part, we learned how to Dockerize a FastAPI machine learning API, build a Docker image, push it to Docker Hub, and run it as a container.
But running the application locally is only one part of the deployment process.
To make our Insurance Premium Prediction API accessible from outside our local machine, we need to deploy it on a server that can be accessed over the internet.
This is where AWS EC2 comes in.
In this final part, we’ll take the Docker image we created in the previous part and deploy it on an AWS EC2 instance.
We’ll go through the complete process:
Docker Image → Docker Hub → AWS EC2 → Docker Container → Live FastAPI API
By the end of this part, our FastAPI machine learning application will be running on an AWS server and accessible through its public network address.
Overview
- Flow Diagram
- Creating an EC2 Instance
- Connecting to the EC2 Instance
- Installing Docker on EC2
- Starting and Enabling Docker
- Configuring Docker User Permissions
- Restarting the EC2 Connection
- Pulling the Docker Image
- Running the Docker Container
- Configuring AWS Security Groups
- Checking the Deployed API
- Connecting the Frontend to the AWS API
- Complete Deployment Workflow
- Key Takeaways
- Conclusion
Flow Diagram

Creating an EC2 Instance
The first step in deploying our FastAPI application on AWS is to create an EC2 instance.
Amazon EC2 (Elastic Compute Cloud) provides a virtual server that we can use to run our Dockerized FastAPI application.
For this project, the EC2 instance will act as the server where we install Docker, pull our Docker image from Docker Hub, and run the FastAPI application.
The deployment process starts with:
AWS EC2 Instance → Docker → Docker Image → FastAPI Container
Create an EC2 Instance
First, create an EC2 instance from the AWS Management Console.
Once the instance is created and running, it will provide a server environment that we can connect to and configure for our application.
The next step is to connect to this EC2 instance and prepare it for running Docker.
Connecting to the EC2 Instance
After creating the EC2 instance, the next step is to connect to the instance so we can configure the server and prepare it to run our Dockerized FastAPI application.
The EC2 instance provides the server environment, while the connection gives us access to its command-line interface.
Once connected, we can install Docker and configure the environment required to run our application.
In the next step, we’ll use the EC2 terminal to install Docker and configure it for our user.
Installing Docker on EC2
Once we are connected to the EC2 instance, the next step is to install Docker.
Our FastAPI application is already packaged as a Docker image. Therefore, Docker needs to be available on the EC2 server so that we can pull and run that image.
Run the following commands in the EC2 terminal:
sudo apt-get updateThis updates the package information available on the server.
Next, install Docker:
sudo apt-get install -y docker.ioThe -y option automatically confirms the installation prompts.
Starting and Enabling Docker
After Docker is installed, we need to start the Docker service:
sudo systemctl start dockerAnd configure Docker to start automatically when the server starts:
sudo systemctl enable dockerConfiguring Docker User Permissions
Next, add the current user to the Docker group:
sudo usermod -aG docker $USERThis updates the user’s Docker group membership.
After running the command, exit the current EC2 session:
exitThis gives us the correct sequence:
Install Docker → Start Docker → Enable Docker → Configure User Permissions → Reconnect to EC2
Restarting the EC2 Connection
After configuring the Docker user permissions, we need to exit the current EC2 session and establish a new connection.
We previously ran:
sudo usermod -aG docker $USERExit the current session:
exitThen reconnect to the EC2 instance.
This step ensures that the updated Docker group permissions are applied to the new session.
Once the new connection is established, we can continue with the next step: pulling our Docker image from Docker Hub.
Deployment flow so far: Create EC2 → Connect → Install Docker → Start & Enable Docker → Configure Permissions → Reconnect
Pulling the Docker Image
After reconnecting to the EC2 instance, the next step is to pull the Docker image that we previously pushed to Docker Hub.
The Docker image contains our FastAPI application and all the dependencies required to run it. Instead of rebuilding the application directly on the EC2 server, we can download the already-built image from Docker Hub.
Run the following command:
docker pull your-docker-username/your-docker-reponame:latestHere:
docker pulldownloads an image from a Docker registry.your-docker-usernameis your Docker Hub username.your-docker-reponameis the repository containing your FastAPI image.latestspecifies the image tag we want to pull.
The command in the project README follows this same structure.
Once the image has been successfully pulled, it is available on the EC2 instance and ready to be started as a Docker container.
Deployment flow: Docker Hub → EC2 → Docker Image → Docker Container
The next step is to run this Docker image on the EC2 instance.
Running the Docker Container
Once the Docker image has been pulled onto the EC2 instance, the next step is to run the image as a Docker container.
Use the following command:
docker run -p 8080:8080 your-docker-username/your-docker-reponame:latestHere:
docker runcreates and starts a container from the specified image.-p 8080:8080maps port 8080 on the EC2 instance to port 8080 inside the container.your-docker-username/your-docker-reponame:latestspecifies the Docker image we pulled from Docker Hub.
This command is the exact deployment command specified in the project README.
Once the container starts successfully, our Dockerized FastAPI application is running on the EC2 instance.
The next step is to configure the AWS Security Group so that requests from outside the EC2 instance can reach the application.
Deployment flow: Docker Hub → EC2 → Docker Image → Docker Container → FastAPI API
Configuring AWS Security Groups
After running the Docker container on the EC2 instance, the application is running on the server. However, we still need to configure the AWS Security Group to allow requests to reach the API from outside the EC2 instance.
A security group controls the network traffic that is allowed to reach an EC2 instance.
For our deployment, we need to configure the appropriate inbound rule for the port on which the Docker container is running.
Once the required port is allowed, requests from outside the EC2 instance can reach our FastAPI application.
The project README lists changing the security group settings as the next deployment step after running the Docker container.
After updating the security group, we can move to the final step: checking the deployed API.
Deployment flow: AWS EC2 → Docker Container → Security Group → Accessible FastAPI API
Checking the Deployed API
After configuring the AWS Security Group, the final step is to check whether our FastAPI application is accessible from outside the EC2 instance.
At this point, the Docker container is running on the EC2 server, and the required network access has been configured.
We can now use the EC2 instance’s public IP address to access the deployed API.
For example:
http://<EC2-PUBLIC-IP>:8080If the deployment is successful, the FastAPI application should return its API response.
The application includes a home endpoint:
@app.get('/')
def home():
return {'message':'Insurance Premium Prediction API'}So visiting the deployed API’s root endpoint should return:
{
"message": "Insurance Premium Prediction API"
}The application also provides a health endpoint:
http://<EC2-PUBLIC-IP>:8080/healthwhich can be used to check the API and model status. The application returns the model version and whether the model is loaded.
For prediction requests, the /predict endpoint accepts the user input and returns the predicted insurance premium category along with confidence and class probabilities.
Connecting the Frontend
Once the API is successfully deployed, the Streamlit frontend can be configured to send requests to the AWS-hosted API instead of the local FastAPI server.
The project’s frontend already contains an API_URL that can be changed to the AWS address.
The frontend then sends the user’s input to the /predict endpoint and displays the predicted category, confidence score, and class probabilities.
Final deployment flow: ML Model → FastAPI → Docker Image → Docker Hub → AWS EC2 → Docker Container → Public API → Streamlit Frontend
Connecting the Frontend to the AWS API
Once the FastAPI API is successfully deployed on AWS, the Streamlit frontend needs to point to the AWS-hosted API instead of the local FastAPI server.
In the current frontend code, the API address is defined through API_URL:
API_URL = "http://127.0.0.1:8000/predict"The comment in the project code indicates that this address should be replaced with the AWS IP address when deploying the frontend.
For example:
API_URL = "http://<EC2-PUBLIC-IP>:8080/predict"The frontend sends the user’s input to this endpoint using a POST request:
response = requests.post(API_URL, json=input_data)It then reads the API response and displays the predicted insurance premium category, confidence score, and class probabilities.
This connects the user interface to the FastAPI application running inside the Docker container on AWS.
Frontend → AWS API → FastAPI → ML Model → Prediction
This completes the connection between our Streamlit frontend and the deployed FastAPI machine learning API.
Complete Deployment Workflow
At this point, we have completed all the steps required to deploy our Dockerized FastAPI machine learning API on AWS.
The complete deployment process can be summarized as:
ML Model → FastAPI → Docker Image → Docker Hub → AWS EC2 → Docker Container → Public API
Here’s how each stage fits together:
- ML Model Our trained machine learning model is loaded by the prediction logic and used to generate insurance premium predictions.
- FastAPI
FastAPI receives the user’s input through the
/predictendpoint, prepares the model features, and returns the prediction response. - Docker Image The FastAPI application and its dependencies are packaged into a Docker image.
- Docker Hub The Docker image is pushed to Docker Hub so that it can be downloaded from another environment.
- AWS EC2 An EC2 instance provides the server environment where we deploy the application.
- Docker Container Docker is installed on the EC2 instance, and the Docker image is pulled and started as a container.
- Public API After configuring the AWS Security Group, the deployed FastAPI application can be accessed through the EC2 instance’s public address.
The frontend can then communicate with the deployed API by replacing its local API address with the AWS API address.
The Final Architecture
User
│
▼
Streamlit Frontend
│
▼
AWS EC2
│
▼
Docker Container
│
▼
FastAPI API
│
▼
ML Prediction Logic
│
▼
Trained ML Model
│
▼
Prediction ResponseThis completes the journey from building a machine learning model to serving it through FastAPI, containerizing it with Docker, and deploying it on AWS.
Key Takeaways
- AWS EC2: Provides the server environment for hosting our FastAPI application.
- Docker on EC2: Docker allows us to run the application using the same containerized environment we created earlier.
- Docker Image: Instead of rebuilding the application on the server, we pull the existing image from Docker Hub.
- Docker Container: The pulled image is started as a container on the EC2 instance.
- Security Groups: AWS Security Group settings allow external requests to reach the deployed application.
- API Verification: After deployment, we can use the EC2 public address to check whether the FastAPI API is accessible.
- Frontend Integration: The Streamlit frontend can be configured to send prediction requests to the AWS-hosted API.
- Complete Deployment: We moved from a locally running machine learning API to a publicly accessible application.
The complete deployment journey is:
ML Model → FastAPI → Docker Image → Docker Hub → AWS EC2 → Docker Container → Public API → Streamlit Frontend
This completes the deployment of our FastAPI machine learning API on AWS.
Conclusion
Deploying a machine learning model is more than simply training it and generating predictions. The model needs to be packaged, served through an API, and deployed in an environment where other applications can access it.
Throughout this series, we built that complete workflow step by step. We started by understanding APIs and FastAPI, learned how to handle requests and validate data with Pydantic, built CRUD operations, and then turned our trained machine learning model into a prediction API.
We then improved the API architecture, containerized it using Docker, pushed the image to Docker Hub, and finally deployed the container on an AWS EC2 instance. The API can then be accessed through the EC2 server, while the Streamlit frontend can communicate with the deployed prediction endpoint.
The complete journey is:
ML Model → FastAPI → Pydantic → Docker → Docker Hub → AWS EC2 → Docker Container → Public API → Frontend
This brings our FastAPI for Machine Learning series to an end.
The goal was not just to learn FastAPI syntax, but to understand how a machine learning model can move from development to a deployable API and eventually become accessible to real applications.
Series: FastAPI for Machine Learning — Part 12 of 12