1. Domain-Routed AI APIs on Cloud Run for Client Applications


    In order to create Domain-Routed AI APIs on Google Cloud Run which client applications can use, you would need to set up a few things on Google Cloud Platform (GCP). Here is a step-by-step guide on how you can achieve this with Pulumi in Python:

    1. Set up a Google Cloud Run service which will host your AI APIs. Google Cloud Run is a managed platform that enables you to run stateless containers invokable via HTTP requests. This is ideal for deploying AI APIs since it abstracts away the infrastructure management, allowing you to focus on the code.

    2. Configure domain mapping for your Cloud Run service. To do this, you would need to have a custom domain and verify it with GCP. Once verified, you can map this domain to your Cloud Run service, allowing client applications to make requests to a user-friendly domain.

    3. Apply proper permissions and service account settings to your Cloud Run service for security and access control.

    Below is a Pulumi program in Python that sets up a Google Cloud Run service and configures a domain mapping for it:

    import pulumi import pulumi_gcp as gcp # Assume you've already verified your domain with Google Cloud. # Set up a Google Cloud Run service cloud_run_service = gcp.cloudrun.Service("ai-api-service", location="us-central1", # Specify the appropriate region for your service template=gcp.cloudrun.ServiceTemplateArgs( spec=gcp.cloudrun.ServiceTemplateSpecArgs( containers=[ gcp.cloudrun.ServiceTemplateSpecContainerArgs( image="gcr.io/my-project/my-ai-api:latest", # Replace with your container image ), ], ), ), ) # Set up domain mapping for your Cloud Run service domain_mapping = gcp.cloudrun.DomainMapping("domain-mapping", location="us-central1", # Must be the same region as your service metadata=gcp.cloudrun.DomainMappingMetadataArgs( namespace="my-namespace", # Set your GCP namespace ), spec=gcp.cloudrun.DomainMappingSpecArgs( route_name=cloud_run_service.name, ), project="my-project-id", # Your GCP project ID name="api.mydomain.com", # Your custom domain ) # Export the domain URL which will be used to access the AI API. pulumi.export("domain_url", domain_mapping.status.resource_records.apply( lambda records: f"https://{records[0].name}" if records else None ))


    • The gcp.cloudrun.Service resource deploys a containerized application to Cloud Run. Replace the image field with the container image URL for your AI API.

    • The gcp.cloudrun.DomainMapping resource maps your custom domain to the Cloud Run service, enabling your client applications to reach the API through the custom domain name.

    • The pulumi.export statement makes the deployed service's domain URL available as an output of your Pulumi stack. It uses the .apply method on a Pulumi Output to map and format the returned DNS record into a proper URL.

    Please make sure to replace "gcr.io/my-project/my-ai-api:latest", "my-project-id", "api.mydomain.com", and "my-namespace" with your actual container image, GCP project ID, domain, and GCP namespace respectively.

    Before running this code, make sure you have the Google Cloud SDK and Pulumi installed and configured on your machine. You also need to authenticate with GCP and verify the domain you plan to use.