Hosted AI APIs with DigitalOcean App Platform
PythonTo host AI APIs with the DigitalOcean App Platform using Pulumi, we will primarily be focusing on using the
digitalocean.App
Pulumi resource. This resource allows you to create and manage applications on the DigitalOcean App Platform, which is a Platform as a Service (PaaS) offering that can run code, Docker containers, and other workloads. It provides easy scaling, management, and security features to deploy web applications or APIs.The
digitalocean.App
resource is flexible and supports various components such as services, static sites, databases, and workers. In this context, we will create a web service that will serve as our AI API. The service typically would be a Docker container that has your AI application, which could be a Flask, Django, FastAPI, or any other Python application capable of serving HTTP requests.Here's a Pulumi program written in Python that creates a DigitalOcean App that could host a simple AI API:
import pulumi import pulumi_digitalocean as digitalocean # Create a DigitalOcean App. ai_app = digitalocean.App("ai-api-app", spec=digitalocean.AppSpecArgs( # Define the services your app will run. It could be one or more services. services=[ # Each service runs a Docker container digitalocean.AppSpecServiceArgs( name="ai-api-service", # A reference to your Docker image stored in the DigitalOcean Container Registry # Or another public or private registry. image=digitalocean.AppSpecServiceImageArgs( repository="your-docker-image-repository", registry_type="DOCKER_HUB", # Example registry type. Change as per your registry. tag="latest", # Or a specific version tag of your Docker image. ), # Service HTTP port. http_port=80, # The environment variables (secrets) for the service. Depends on your app requirements. envs=[ digitalocean.AppSpecServiceEnvArgs( key="API_KEY", scope="RUN_TIME", type="SECRET", # Marking this environment variable as a secret. value=pulumi.Config("ai-api").require_secret("API_KEY") # Retrieve from Pulumi config. ) ], # Number of instances running your service. instance_count=1, # Size of the instances running your service. instance_size_slug="basic-xxs" # This is the smallest size, adjust according to the needs. ) ], # The region where the app will be deployed. region="nyc", # New York data center, choose the region closest to your users. ) ) # Export the default domain of the app, once deployed. pulumi.export("ai_api_url", ai_app.default_domain)
In the above program:
- We initiate a
digitalocean.App
resource dubbedai-api-app
. - Within the app spec (
AppSpecArgs
), we define a single service to represent our AI API service. - The API uses a Docker image which should contain your AI application.
- We set an HTTP port, which is the port the service inside the container will listen on for HTTP requests.
- The environment variables can be configured based on your application's needs. In this example, an
API_KEY
is configured which is marked as a secret and retrieved from the Pulumi config. - The
instance_count
andinstance_size_slug
define the scaling and size of the service. These should be adjusted based on the expected load and performance requirements. - Finally, we select a region that is geographically closest to the target users for the best latency.
Please make sure you've prepared the following before running this code:
- A Docker image with your AI application, tagged and pushed to a container registry that DigitalOcean can access (e.g., Docker Hub, DigitalOcean Container Registry).
- An access token with appropriate permissions to manage the DigitalOcean resources.
- Pulumi CLI installed and configured with the required credentials.
When you run
pulumi up
with this code, Pulumi will provision the resources required to host your AI API on the DigitalOcean App Platform. Theai_api_url
export is the URL where your service can be accessed once the deployment is successful.Remember to replace
"your-docker-image-repository"
with the actual repository where your Docker image is stored and adjust other properties as needed for your use case.- We initiate a