Custom Domain Mapping for AI Model Serving with Cloud Run
PythonIn this scenario, we want to deploy a Google Cloud Run service that serves an AI model and map a custom domain to it. Google Cloud Run is a managed platform that enables you to run stateless containers that are invocable via web requests or Pub/Sub events. Custom domain mapping is the process of associating a domain that you own with the Cloud Run service so that it's accessible via the custom domain instead of the default URL provided by Cloud Run.
To achieve this, we will use Pulumi to perform the following steps:
- Deploy a Cloud Run service with a container that serves your AI model.
- Configure and map a custom domain to the Cloud Run service.
In the Pulumi program below, replace
YOUR_CUSTOM_DOMAIN
with your actual domain, andIMAGE_URL
with the URL of the container image for your AI model. This container image should be stored in a location that your Google Cloud Run service can access, such as Google Container Registry (GCR) or Google Artifact Registry.Now, let's write the Pulumi program to accomplish these tasks:
import pulumi import pulumi_gcp as gcp # Replace these variables with your actual data. custom_domain = "YOUR_CUSTOM_DOMAIN" # Custom domain to map to Cloud Run image_url = "IMAGE_URL" # URL of the container image for the AI model # Create a Cloud Run service. cloud_run_service = gcp.cloudrun.Service("model-serving-service", location="us-central1", template=gcp.cloudrun.ServiceTemplateArgs( spec=gcp.cloudrun.ServiceTemplateSpecArgs( containers=[gcp.cloudrun.ServiceTemplateSpecContainerArgs( image=image_url, )], ), )) # Create a domain mapping for the Cloud Run service. domain_mapping = gcp.cloudrun.DomainMapping("custom-domain-mapping", location=cloud_run_service.location, metadata=gcp.cloudrun.DomainMappingMetadataArgs( namespace=cloud_run_service.statuses[0].conditions[0].message, ), spec=gcp.cloudrun.DomainMappingSpecArgs( route_name=cloud_run_service.statuses[0].conditions[0].message, ), name=custom_domain, ) # Export the URL to the Cloud Run service. pulumi.export("cloud_run_url", cloud_run_service.statuses[0].url) # Export the custom domain that has been mapped to the Cloud Run service. pulumi.export("custom_domain_url", domain_mapping.status[0].resource_records[0].rrdata)
This program deploys a Cloud Run service with the specified container image and maps the provided custom domain to this service. It uses two Pulumi resources:
gcp.cloudrun.Service
for deploying the service andgcp.cloudrun.DomainMapping
for mapping the custom domain.- The
gcp.cloudrun.Service
resource deploys a new instance of Cloud Run with a container that serves your AI model. Replaceus-central1
with your desired Google Cloud location. - The
gcp.cloudrun.DomainMapping
resource configures the domain mapping for our Cloud Run service. We pass in theroute_name
from the Cloud Run service, which automatically links the custom domain to the appropriate service. - Finally, we export two outputs
cloud_run_url
andcustom_domain_url
which represent the Cloud Run service URL and the custom domain URL, respectively.
Please ensure that you have the proper permission to map the custom domain and that you have set up the domain in Google Cloud DNS or another DNS provider to point to the Cloud Run service.
Remember to replace
YOUR_CUSTOM_DOMAIN
andIMAGE_URL
with your actual domain and container image URL before you run this program with Pulumi.