Skip to content

Example of a FastAPI server with application load balancing in AWS

License

Notifications You must be signed in to change notification settings

mcleantom/fastapi-terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI - Terraform

This repository shows an example template of deploying a FastAPI instance to an AWS that is:

  • Isolated in a virtual-private-cloud (VPC)
  • Load balanced
  • Auto scaled
  • Secured by SSL
  • DNS routed with Route53
  • Accessible by SSH
  • Continuously deployed with ECR

It is based on the example Terraform AWS VPC Example, with a few modifications to use AWS ECR to host the docker containers that will run the FastAPI instances, as well as the auto scaling group automatically replacing EC2 instances when a new docker image is pushed to the ECR repository.

How do I get setup?

Docker

Docker will be used to containerize the FastAPI apps. Follow the instructions to install for your OS

Terraform

To install terraform, follow the installation instructions for your OS. Alternatively, to install terraform on linux, run the shell commands:

sudo apt update
sudo apt install  software-properties-common gnupg2 curl
curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpg
sudo install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com focal main"
sudo apt update
sudo apt install terraform

IAM User with Permissions

  • Create an IAM group called TerraformUsers
  • Attach the policy PowerUserAccess
  • Add your IAM users to the group

Hosted zone with a wildcard simple record

Follow this stackoverflow answer to create a wildcard simple record for your hosted zone.

An ECR repository.

Follow this tutorial To create a repository in AWS to push your docker images to.

AWS CLI

We will use AWS CLI to push docker images from our computer to AWS. Follow the tutorial for your OS

Configure the project properties

Fill in the appropriate variables in the user.tfvars file.

Deployment

First, initialize terraform

terraform init

then check the plan

terraform plan

deploy the plan to AWS.

terraform apply -var-file="user.tfvars"

then, make sure to destroy your deployment after testing to avoid charges.

terraform destroy -var-file="user.tfvars"

About

Example of a FastAPI server with application load balancing in AWS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published