Deploy via Python SDK

👍

What you'll learn

  • How to Deploy your Async Service using the Python SDK

Upon completing this guide, you will have successfully deployed an Async Service. Your Async Service deployment dashboard will resemble the following:

Step 1: Clone the code

To get started, clone the following repository by entering the following command in your terminal:

git clone https://github.com/truefoundry/getting-started-examples

After cloning the repository, navigate to the code directory using the following command:

cd deploy-ml-model

The deploy.py script that we'll create in the upcoming steps will be located in this directory.

Step 2: Deploying the Async Service

In this step, we will create a deploy.py file to configure and deploy our Async Service. The deploy.py file will contain the code that defines the necessary configuration for your Async Service.

Please create the deploy.py file in the same directory where your Async Service code (app.py) is located. After creating the deploy.py file, your directory structure will look like the following:

File Structure

.
├── app.py
├── deploy.py
└── requirements.txt

deploy.py

import argparse
import logging
from servicefoundry import AsyncService, PythonBuild, Port, Resources, WorkerConfig, SQSInputConfig, SQSOutputConfig, AWSAccessKeyAuth, AsyncProcessorSidecar

logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser()
parser.add_argument("--workspace_fqn", required=True, type=str)
parser.add_argument("--host", required=True, type=str)
args = parser.parse_args()

# Define your AWS access credentials (replace placeholders with actual values)
aws_access_key_id = "<YOUR_AWS_ACCESS_KEY_ID>"
aws_secret_access_key = "<YOUR_AWS_SECRET_ACCESS_KEY>"

# Define your AWS SQS input and output queue URLs (replace placeholders with actual URLs)
input_queue_url = "<YOUR_INPUT_SQS_URL>"
output_queue_url = "<YOUR_OUTPUT_SQS_URL>"

# Define your AWS region name (replace placeholders with your region)
region_name = "<YOUR_REGION_NAME>"

async_service = AsyncService(
    name="async-service",
    image=PythonBuild(
        command="gunicorn app:app --workers 1 --worker-class uvicorn.workers.UvicornWorker --bind 127.0.0.1:8000",
        requirements_path="requirements.txt",
    ),
    worker_config=WorkerConfig(
        input_config=SQSInputConfig(
            queue_url=input_queue_url,
            region_name=region_name,
            visibility_timeout=2,
            wait_time_seconds=1,
            auth=AWSAccessKeyAuth(
                aws_access_key_id=aws_access_key_id,
                aws_secret_access_key=aws_secret_access_key,
            ),
        ),
        output_config=SQSOutputConfig(
            queue_url=output_queue_url,
            region_name=region_name,
            auth=AWSAccessKeyAuth(
                aws_access_key_id=aws_access_key_id,
                aws_secret_access_key=aws_secret_access_key,
            ),
        ),
    ),
    sidecar=AsyncProcessorSidecar(
      destination_url=your_destination_url,
    )
    ports=[
        Port(
            port=8000,
            host=args.host,
        )
    ],
    resources=Resources(
        cpu_request=0.5,
        cpu_limit=1,
        memory_request=200,
        memory_limit=500,
        ephemeral_storage_request=1000,
        ephemeral_storage_limit=2000,
    ),
)

async_service.deploy(workspace_fqn=args.workspace_fqn)

To deploy using Python SDK use:

python deploy.py --workspace_fqn <YOUR WORKSPACE FQN HERE> --host <YOUR HOST>

Run the above command from the same directory containing the app.py and requirements.txt files.

📘

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

After executing the command mentioned above, the deployment process will begin.
Please be patient and wait for the deployment process to finish. You can monitor the deployment status, and once it displays "SUCCESS", your Async Service will be successfully deployed.

Once the deployment process is complete, you will receive a link to access the dashboard. Look for a message like You can find the application on the dashboard: in the output.
Click on the provided link to access the deployment dashboard.

Interact with your Async Service

Follow the following guide to learn how to Interact with the Async Service you just deployed:
Interacting with your Async Service