The purpose of this project is to help you to quickly deploy a FastAPI on AWS - Lambda using Terraform. Thanks to @Sancho66 for his help.
To start you will need to install terraform and setup your aws credentials with aws cli. Today AWS - Lambda, latest python version is 3.8, so this is the one we use.
To generate you local venv:
python3.8 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
To start the fast api locally you need to connect to your python venv
and start your api with uvicorn.
Go to app
directory and execute uvicorn main:app --reload
source venv/bin/activate
cd app
uvicorn main:app --reload
Now on http://127.0.0.1:8000 you will have your api and on http://127.0.0.1:8000/docs the swagger documentation for your api.
The deployment will creat:
- A random pet name bucket where will be stored the packaged lambda.
- An AWS lambda with your FastAPI.
- An AWS role to be attached to the lambda.
- The AWS API Gateway which will trigger the lambda.
First you will need to edit your AWS region in my case
eu-central-1
. To do it edit infrastructure/variables.tf
and change
the aws_region
variable value.
To init terraform execute make init
Now you just have to execute make deploy
and it will do the job.
Answer yes
to the question Do you want to perform these actions?
If everything gone alright at the end you will have something like:
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
Outputs:
api_endpoint = "https://[my_api_subdomain].execute-api.eu-central-1.amazonaws.com"
lambda_bucket_name = "terraform-fast-api-func-[4_pets_name]"
By clicking the api_endpoint url you should have
{"status":"OK","message":"Et VOILA !"}
as answer.
To access to the swagger of the api you can go to: https://[my_api_subdomain].execute-api.eu-central-1.amazonaws.com/docs