Getting Started
In this guide, we’ll deploy a FastAPI service for solving the Iris classification problem. The problem involves predicting the species of an iris flower based on its sepal length, sepal width, petal length, and petal width. There are three species: Iris setosa, Iris versicolor, and Iris virginica.
We’ve already created a FastAPI service for the Iris classification problem, and you can find the code in our GitHub Repository.
Please visit the repository to familiarize yourself with the code you’ll be deploying. The project files are organized as follows:
Getting Started With Deployment
To deploy a service, you’ll need a workspace. If you don’t have one, you can create it using this guide: Creating a Workspace or seek assistance from your cluster administrator in case you don’t have permission to create a workspace.
In Truefoundry, you can either deploy code from your Github repository or from your local machine in case the code is not pushed to a Github repository.
Use these configs for the deployment form:
- Repo URL: https://github.com/truefoundry/getting-started-examples
- Path to build context: ./deploy-model-with-fastapi/
- Command: uvicorn app:app —host 0.0.0.0 —port 8000
- Port: 8000
What we did above:
In the example above, we only had Python code and a requirements.txt. We didn’t have a prewritten docker file - so we chose the Python Code option - to let TrueFoundry templatize a Dockerfile from the details provided about the application and build the Docker image for us.
We give these details in the Build context field, where we specify the directory in the GitHub repository where our service code resides (./deploy-model-with-fastapi/
). We also specify the command that we need to use to run our service (uvicorn app:app --host 0.0.0.0 --port 8000
).
Finally, we specify the port that we want our service to listen on (8000
).
Use these configs for the deployment form:
- Repo URL: https://github.com/truefoundry/getting-started-examples
- Path to build context: ./deploy-model-with-fastapi/
- Command: uvicorn app:app —host 0.0.0.0 —port 8000
- Port: 8000
What we did above:
In the example above, we only had Python code and a requirements.txt. We didn’t have a prewritten docker file - so we chose the Python Code option - to let TrueFoundry templatize a Dockerfile from the details provided about the application and build the Docker image for us.
We give these details in the Build context field, where we specify the directory in the GitHub repository where our service code resides (./deploy-model-with-fastapi/
). We also specify the command that we need to use to run our service (uvicorn app:app --host 0.0.0.0 --port 8000
).
Finally, we specify the port that we want our service to listen on (8000
).
To deploy from your local machine, you can follow the steps on the UI to get the deployment script.
Once you reach the last step, you will be able to download a deploy.py script that contains the configuration for the deployment. You should place the deploy.py script in the root of your project.
The deploy.py should be placed in the root of your project.
The deploy.py has a field called build_context_path
. The build_context_path is considered relative to the location of the deploy.py file.
All the code in the build_context_path
will be packaged into a docker image and deployed.
The directory structure will then appear as follows:
To deploy, execute the command:
You should be in the same directory as the deploy.py file when you run the command.
Explanation of the deploy.py script
deploy.py
Picking a value for host
Providing a host value depends on the base domain urls configured in the cluster settings, you can learn how to find the base domain urls available to you here
For e.g. If your base domain url is *.truefoundry.your-org.com
then a valid value can be fastapi-your-workspace-8000.truefoundry.your-org.com
.
Alternatively if you have a non wildcard based domain url e.g. truefoundry.your-org.com
, then a valid value can be truefoundry.your-org.com/fastapi-your-workspace-8000
To understand the code, you can click the following recipe:
To deploy using Python SDK use:
Run the above command from the same directory containing the app.py
and requirements.txt
files.
Exclude files when building and deploying your source code:
To exclude specific files from being built and deployed, create a .tfyignore file in the directory containing your deployment script (deploy.py
). The .tfyignore
file follows the same rules as the .gitignore
file.
If your repository already has a .gitignore
file, you don’t need to create a .tfyignore
file. Service Foundry will automatically detect the files to ignore.
Place the .tfyignore
file in the project’s root directory, alongside deploy.py.
After running the command mentioned above, wait for the deployment process to complete. Monitor the status until it shows DEPLOY_SUCCESS:
, indicating a successful deployment.
Once deployed, you’ll receive a dashboard access link in the output, typically mentioned as You can find the application on the dashboard:
. Click this link to access the deployment dashboard.
View your deployed service
Congratulations! You’ve successfully deployed your FastAPI service.
Once you click Submit
, your deployment will be successful in a few seconds, and your service will be displayed as active (green), indicating that it’s up and running.
You can view all the information about your service following the steps below:
Copy Endpoint URL
To make a request to the Service, you will need the Endpoint URL.
The endpoint URL is the same one you provided while deploying the service in the Ports Section. You can also copy it from the Service UI.
The endpoint will be an internal cluster URL or an external URL based on whether you chose Expose in the Ports configuration. The two cases are as follows:
The service port is exposed and mapped to a domain
In this case, this becomes a public endpoint and you can call this service from anywhere - your own laptop or code running anywhere.
You can add username-password-based authentication to the API in case you don’t want everyone to be able to call this API. For this, you can refer to the following section Add Authentication to Endpoint.
The service port is not exposed
If you have not exposed the port, the endpoint is internal to the cluster and can only be called by other services running in the same cluster (including Jupyter Notebooks running in the same cluster). No one externally can access this service and this is the recommended mode for APIs that don’t need to be exposed for external usage. These APIs will be of the format servicename-workspacename.svc.cluster.local:port
Sending Requests to your Service
Once you deploy the service, you will want to interact with the API in your code or manually using curl or Postman or Python code as shown below: