Git Product home page Git Product logo

fastapi-walle's Introduction

FastAPI Tutorial

This is a simple example FastAPI application that pretends to be a bookstore.

Deploying to AWS EC2

Log into your AWS account and create an EC2 instance (t2.micro), using the latest stable Ubuntu Linux AMI.

SSH into the instance and run these commands to update the software repository and install our dependencies.

sudo apt-get update
sudo apt install -y python3-pip nginx

Clone the FastAPI server app (or create your main.py in Python).

git clone https://github.com/pixegami/fastapi-tutorial.git

Add the FastAPI configuration to NGINX's folder. Create a file called fastapi_nginx (like the one in this repository).

sudo vim /etc/nginx/sites-enabled/fastapi_nginx

And put this config into the file (replace the IP address with your EC2 instance's public IP):

server {
    listen 80;   
    server_name <YOUR_EC2_IP>;    
    location / {        
        proxy_pass http://127.0.0.1:8000;    
    }
}

Start NGINX.

sudo service nginx restart

Start FastAPI.

cd fastapi-tutorial
python3 -m uvicorn main:app

Update EC2 security-group settings for your instance to allow HTTP traffic to port 80.

Now when you visit your public IP of the instance, you should be able to access your API.

Deploying FastAPI to AWS Lambda

We'll need to modify the API so that it has a Lambda handler. Use Mangum:

from mangum import Mangum

app = FastAPI()
handler = Mangum(app)

We'll also need to install the dependencies into a local directory so we can zip it up.

pip install -t lib -r requirements.txt

We now need to zip it up.

(cd lib; zip ../lambda_function.zip -r .)

Now add our FastAPI file and the JSON file.

zip lambda_function.zip -u main.py
zip lambda_function.zip -u books.json

fastapi-walle's People

Contributors

pixegami avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.