Virendra Oswal
Virendra Oswal's Blog

Virendra Oswal's Blog

FastAPI Deployment with Deta.sh Cloud

FastAPI Deployment with Deta.sh Cloud

Deploy FastAPI app in seconds using Deta.sh!

Virendra Oswal's photo
Virendra Oswal

Published on Aug 14, 2021

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Be it a side project or some Proof of Concept, we want to get our services deployed and accessible as soon as possible as a developer. Also, we want to avoid the hustle of managing infrastructure when there is load and vice versa.

Platform as a Service helps solve that problem, and today we will cover another one that is slowly gaining momentum in Python space named Deta.sh.

This is the first post in Deta.sh series, where we will see how to host an API using FastAPI, a rapidly growing Python web framework with ease and no hustle.

Pre-requisites

  • FastAPI Service. (We will create one in this blog, for demo purposes.)
  • Deta.sh account. (Free to signup, their verification mail looks like spam though :D )

On account creation, it will create the default project for grouping your micros. You can create a new Project if required from the dashboard, we will use default in this case.

What is Micro?

As per official documentation

Deta Micros is a simple yet powerful environment for running code in the cloud. Micros are both highly performant and scalable while coming with an HTTP endpoint and authentication out of the box.

Deta.sh readiness with FastAPI

Install Deta.sh CLI

We will install Deta.sh CLI, so it becomes easy to work with deployment, CLI for different OS can be found here We are on Windows 10, we will execute the below command in PowerShell

iwr https://get.deta.dev/cli.ps1 -useb | iex

Once you execute, if successful installation you will see a message like below: deta_cli_success.JPG

Creating first Python Micro under Default project

Before creating the First micro, we need to login for authentication purpose, its simple thought we execute below command in CLI:
deta login

On executing, the browser will open Sign In or Sign Up if no account, post that on CLI you will see successful authentication.

deta --help will give you CLI options to play around with.

We will execute the below command to create the first Python Micro under the default project
deta new --python fastapi-micro --project default

On successful execution, it will create micro in the cloud and also a directory with the same name in your local. micro-create-Success.JPG The same micro should be visible in the cloud too micro-create-Success_web.JPG

If we look inside folder fastapi-micro, in local it has one directory named .deta/ which kind of holds metadata about Micro we created and main.py which acts as an entry point for our application.

Adding FastAPI

Now we have micro ready, let's add FastAPI before we can deploy update Code back to our Micro in the cloud.

Note

For Deta to be able to run your code, you need to call your app instance app and it has to be in a file called main.py, which is the only required file for a Python Micro. Of course, you could add more files and folders.

Unlike Heroku where you have to specify uvicorn command as part of Procfile to run FastAPI application, we don't need to specify anything in Deta. They host their own web server, we just need to have main.py, app instance, and requirements.txt specifying dependency which is the usual python module dependency approach.

Updated main.py to run simple FastAPI endpoint

from fastapi import FastAPI
app = FastAPI()  # notice that the app instance is called `app`, this is very important.

@app.get("/")
async def read_root():
    return {"message": "Hello from Deta.sh via FastAPI"}

Updated requirements.txt to add fastapi dependency:

fastapi

Let's deploy all update code to the cloud, by executing the below command:
deta deploy

On trigger, it will pull in required dependencies and deploy an app with changes deta_deploy.JPG If all is good, we can visit the random name endpoint created. To view the endpoint, we can visit Micro on Dash.sh dashboard or execute the below command where micro is present locally:
deta details deta-details.JPG It shares good informative details about micro, especially endpoint.

Others are name, runtime, dependencies which are self-explanatory.

Visor is enabled by default offers a user interface for you to see a live view of all the events (logs) processed by your Micro. You can use it to monitor and debug your requests, test your endpoints by sending requests, and more Visor.JPG

http_auth determines if API is public enabled, it's disabled by default.

If we hit endpoint in the browser where our user credentials for deta.sh stored in this case as cookies are enabled bypkhn.deta.dev it will show the message as below which is we are able to access, Voila.

access_woorking.JPG

If you get unauthorized access, we can enable HTTP auth using the below command: deta auth disable

Deta.sh is easy to set up cloud providers to host python and node services, and they continue to offer a lot of other features bar Micro, specially Deta Base acts as Database, Deta Drive acting as low-key AWS S3, and Deta Access for permissions.

The best thing is the only Pricing Plan which is Free for now, so its time to make use of it in Hackathons, POCs, Side projects with no restriction

We will explore other features of this cloud and see how we can make PROD-ready applications with ease. Obviously few steps are the first time, post that it's an easy ride. In the next article in the series, we will see how we can set up CI/CD using Deta Deploy Button via Github on the above code itself.

Resouces

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

 
Share this