Using gcp eventarc with vertex
TypeScriptTo integrate Google Cloud's Eventarc with Vertex AI, you'll create an Eventarc trigger that watches for specific events and sends them to a Vertex AI endpoint for processing. This is useful if you want to perform machine learning predictions or other actions in response to events happening within your Google Cloud environment.
Let's go through the steps to create this setup using Pulumi and the Google Cloud (GCP) provider. The main resources we'll use are:
gcp.eventarc.Trigger
: The Eventarc trigger that fires when a specified event occurs.gcp.vertex.AiEndpoint
: The Vertex AI endpoint that will handle the event sent by the trigger.
Below is a program written in TypeScript that sets up an Eventarc trigger that sends events to a Vertex AI endpoint.
First, ensure you have the Pulumi CLI and Google Cloud SDK installed and properly set up with the necessary access rights.
Here's the Pulumi program:
import * as pulumi from '@pulumi/pulumi'; import * as gcp from '@pulumi/gcp'; // Configure the Google Cloud provider settings, such as project, region, etc. const config = new pulumi.Config('gcp'); const project = config.require('project'); const location = config.require('location'); // Create a Vertex AI Endpoint. For simplicity, we're creating a new endpoint, // but typically, you'd use an existing endpoint where your model is deployed. const aiEndpoint = new gcp.vertex.AiEndpoint('myAiEndpoint', { project, location, displayName: 'My AI Endpoint', // other properties like the model to deploy might be necessary }); // Create an Eventarc trigger that will react to a specific event // and send the event to the Vertex AI endpoint created above. const trigger = new gcp.eventarc.Trigger('myTrigger', { project, location, destination: { cloudRunService: { // Assuming that the AI endpoint is exposed as a Cloud Run service service: aiEndpoint.name, path: '/predict', // The path where prediction requests should be sent region: location, }, }, matchingCriterias: [{ attribute: 'type', // The type of event to match, e.g., google.cloud.storage.object.v1.finalized value: 'YourEventType', // Replace with the actual event type you want to listen for }], serviceAccount: 'YourServiceAccount', // Service account with permissions to invoke the AI endpoint }); // Export the URL of the AI Endpoint to which Eventarc will send events export const aiEndpointUrl = aiEndpoint.name.apply(name => `https://${location}-aiplatform.googleapis.com/v1/${name}:predict`); // Export the name of the Eventarc Trigger export const triggerName = trigger.name;
Here's the detailed breakdown of what this program does:
- It imports the necessary Pulumi libraries for interacting with Google Cloud resources.
- It reads the GCP configuration such as project and location. This assumes that you've already configured these values using the
pulumi config set gcp:project <PROJECT_ID>
andpulumi config set gcp:location <LOCATION>
. - It creates a Vertex AI endpoint. The
displayName
property gives a name to the endpoint. If you already have an AI model deployed on Vertex AI, you would connect to an existing endpoint rather than creating a new one. - It sets up an Eventarc trigger that reacts to events (specified in
matchingCriterias
) and directs these to the Vertex AI endpoint. - The Eventarc trigger is configured with a destination that points to our AI endpoint. The
path
is the URI for prediction requests, but this may need to be adjusted according to your own API. - It provides the
serviceAccount
with permissions to invoke the AI endpoint. - Finally, the program exports the URL of the AI endpoint and the name of the Eventarc trigger, which can be helpful for debugging or for use in other CI/CD or monitoring tools.
Remember to replace
YourEventType
andYourServiceAccount
with the specific event you're listening for and the service account that has the necessary permissions to invoke the AI endpoint, respectively.To deploy this configuration, save the code in a file called
index.ts
, then run the following Pulumi CLI commands:pulumi up
This command will prompt you to review and confirm the resources that will be created or modified before any action is taken. After confirming, Pulumi applies the changes and provisions the described resources in your GCP project.