Deploying FastAPI Web Application in AWS
A step by step guide to deploy FastAPI application in AWS.
In this article, I would like to share my learnings on deploying FastAPI web application in AWS. More often than not, we, as data scientists, focus on building complex machine learning models, and always rely on data engineers for deployment. However, our machine learning model serves well only if it can be accessed interactively through a web application.
To this extent, I have outlined below, the necessary four steps to deploy FastAPI application in AWS:
1. Create FastAPI Application
2. Dockerize the Fast API application
3. Push the Docker Image to AWS ECR
4. Deploy FastAPI application in EC2
I will use simple hello world FastAPI application for the purpose of demonstration.
Lets do it 👍
Step 1:Create FastAPI Application
i) Create fast api
ii) Run the server using command: uvicorn main:app — reload
When you can see the following output on your console, it means that your app is successfully deployed locally.
iii) Open your browser at http://127.0.0.1:8000/
You should be able see the following output :
{"Hello":"World"}
Now you have successfully build fast api app locally.
You can read more about FastAPI here.
Step 2: Dockerize the Fast API application
To dockerize the application, first you should create a Dockerfile
Dockerfiles describe how to assemble a private filesystem for a container, and can also contain some metadata describing how to run a container based on the image.
i) Create a docker file
ii) Build Docker Image:
docker build -t fastapi:latest .
iii) Run the docker image
docker run -p 5000:5000 api:latest
iv) Open your browser at http://localhost:5000
you should be able see the following output
{"Hello":"World"}
Now you have successfully developed Fast API application in docker container.
You can learn more about docker here
Step 3:Push the Docker Image to AWS ECR
Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images.
Firstly you need to install and configure AWS CLI to push the docker images to AWS ECR
i) Install the AWS CLI:
Run the following two commands to install AWS CLI
curl “https://awscli.amazonaws.com/AWSCLIV2.pkg" -o “AWSCLIV2.pkg”sudo installer -pkg AWSCLIV2.pkg -target /
To Know more details about installation of AWS CLI, please go through the following link.
ii) Verify the installation by running the following command
aws --version
To configure the AWS CLI , get access key ID and Secret Access Key from Identity and Access Management (IAM) in AWS
iii) Click on Manage Security Credentials.
iv) Click on create new access key and store the access keys for future purposes.
v) Configure the AWS CLI:
aws configureAWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]:json
vi) After configuring AWS CLI, login into AWS ECR using the following command, provide region and account id details.
aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
You should be able see “Login Succeeded” on the console if everything goes well.
vii)Create Repository:
You can create repository either in CLI or directly in AWS console
aws ecr create-repository --repository-name fast_api_app
https://console.aws.amazon.com/ecr/repositories
viii)Tag your image so you can push the image to this repository
docker tag api:latest aws_account_id.dkr.ecr.region.amazonaws.com/fast_api_app
ix)Run the following command to push the docker image to AWS repository
docker push aws_account_id.dkr.ecr.region.amazonaws.com/fast_api_app
Step 4: Deploy FastAPI application in EC2
After pushing docker image to AWS ECR , you have to create Amazon EC2 instance which can serve your web application.AWS offers many instances in the free tier, I have chosen Linux instance here(you can use other instances as well but do remember to the change the configuration accordingly)
i ) Go to Amazon Console; select Amazon Linux EC2 instance.
ii) Choose general purpose t2.micro instance type as shown below.
iii) Change the security settings by adding rule using custom TCP as type and port as 5000 and click on review and launch instance. These configurations are necessary for our web application to run
iv) Create a new key pair by selecting from the dropdown “Create a new key pair”, provide name for the key pair, and download it.
A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance.
You should able to see instance that’s running successfully as shown below.
Once you download the key pair, pem file is generated, ssh into amazon linux instance by using this pem file.
v) Log into ec2 instance by using following command
ssh -i fastapi.pem ec2-user@ec2-18-221-11-226.us-east-2.compute.amazonaws.com
vi) Install the docker in Linux Machine.
sudo yum install -y docker
vii) Configure AWS as you did earlier.
aws configureAWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]:json
viii) Run the following commands to add ec2 user to perform docker commands in Linux machine.
sudo groupadd dockersudo gpasswd -a ${USER} dockersudo service docker restart
ix) Exit the instance and ssh in to EC2 instance again .
ssh -i fastapi.pem ec2-user@ec2-18-221-11-226.us-east-2.compute.amazonaws.com
x) Log into the Amazon ECR registry:
aws ecr get-login --region region --no-include-email
output
docker login -u AWS -p <password> -e none https://<aws_account_id>.dkr.ecr.<region>.amazonaws.com
xi) Copy the output from the above and run it in the command line. Once you are successfully logged into AWS ECR, you can see “Login Succeeded” in the console.
xii) Pull the docker image from AWS ECR.
docker pull aws_account_id.dkr.ecr.region.amazonaws.com/fast_api_app:latest
xiii) Run the docker container in Linux Machine
docker run -p 5000:5000 aws_account_id.dkr.ecr.region.amazonaws.com/fast_api_app
xiv) Get the IPv4 Public IP from the instance details page and add port 5000 while launching it in the browser.
Hurray! Our Web Application is finally running in AWS.
Note: Please delete all the instances and repositories created in Amazon Web Services after you deploy it unless you want to maintain it for public usage.
Happy Learning 👏