Set permissions for generative AI functions that call Vertex AI models
This document shows you how to set up permissions for running generative AI
queries. Generative AI queries contain AI.* functions that call foundation
models in Vertex AI; for example, AI.GENERATE.
There are two ways to set up permissions to run queries that use AI.*
functions:
- Run the query using your end-user credentials
- Create a connection to run the query using a service account
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Enable the BigQuery API, BigQuery Connection API, and Vertex AI API APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.For new projects, the BigQuery API is automatically enabled.
- Optional: Enable billing for the project. If you don't want to enable billing or provide a credit card, the steps in this document still work. BigQuery provides you a sandbox to perform the steps. For more information, see Enable the BigQuery sandbox.
Required roles
To get the permissions that you need to run a query job that calls a Vertex AI model, ask your administrator to grant you the following IAM roles on the project:
-
Run query jobs:
BigQuery Job User (
roles/bigquery.jobUser) -
Create a connection:
BigQuery Connection Admin (
roles/bigquery.connectionAdmin) -
Access a remote model in Vertex AI:
Vertex AI User (
roles/aiplatform.user)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Run generative AI queries with end-user credentials
For user-initiated queries, the CONNECTION argument is optional. When a
user initiates a query, BigQuery ML uses the credentials of the user who submitted the
query to run it.
If your query job is expected to run for 48 hours or longer, you should use the
CONNECTION argument to run the query using a service account.
Required roles
To run a generative AI query that uses AI.* functions to call a Vertex AI
model, the user or group must be granted the following roles.
To get the permissions that you need to run a query job that calls a Vertex AI model, ask your administrator to grant you the following IAM roles on the project:
-
Run query jobs:
BigQuery Job User (
roles/bigquery.jobUser) -
Access a remote model in Vertex AI:
Vertex AI User (
roles/aiplatform.user)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Note that additional permissions are required to grant roles to a user. For more information, see Required roles on the Manage access to projects, folders, and organizations page.
If the user running the query doesn't have the required permissions, you may see
an error similar to the following: The user does not have permission to access
resources used by <var>FUNCTION_NAME</var>.
Grant the required roles to the user or group
You can use the Google Cloud console to grant the required roles for a
principal. The principal is the user or group that runs the query that uses
AI.* functions to call a Vertex AI foundation model.
In the Google Cloud console, go to the IAM page.
Select your project.
To modify roles for a principal who already has roles on the project:
Find the row that contains the principal, and then click Edit principal.
In the Assign roles section, click Add another role.
For Select another role, click the drop-down arrow.
Search for or browse to the Vertex AI User role and select it.
Click Add another role.
In the Assign roles section, for Select another role, click the drop-down arrow.
Search for or browse to the BigQuery Job User role and select it.
Click Save.
To grant roles to a principal who doesn't have any roles on the project:
Go to the IAM & Admin page.
Click Grant access.
The Add principals dialog opens.
In the New principals field, enter the principal identifier— for example,
my-user@example.comor//iam.googleapis.com/locations/global/workforcePools/example-pool/group/example-group@example.com.In the Assign roles section, for Select a role, click the drop-down arrow.
Search for the Vertex AI User role and select it.
Click Add another role.
In the Assign roles section, for Select a role, click the drop-down arrow.
Search for or browse to the BigQuery Job User role and select it.
Click Save.
For information on other methods of granting project-level roles to a principal, see Grant or revoke multiple IAM roles programmatically.
Run generative AI queries with a Cloud Resource Connection
To run generative AI queries using a connection, create the connection, and then grant access to the service account created by the connection.
Create a connection
You can set up a Cloud Resource Connection to run all generative AI queries that
contain AI.* functions. When you create a connection, you grant permissions to
run queries to a service account. Creating a connection is required for
background jobs such as vector search indexing and is recommended for jobs that
run longer than 48 hours.
Select one of the following options:
Console
Go to the BigQuery page.
In the Explorer pane, click Add data:
The Add data dialog opens.
In the Filter By pane, in the Data Source Type section, select Business Applications.
Alternatively, in the Search for data sources field, you can enter
Vertex AI.In the Featured data sources section, click Vertex AI.
Click the Vertex AI Models: BigQuery Federation solution card.
In the Connection type list, select Vertex AI remote models, remote functions, BigLake and Spanner (Cloud Resource).
In the Connection ID field, enter a name for your connection.
Click Create connection.
Click Go to connection.
In the Connection info pane, copy the service account ID for use in a later step.
bq
In a command-line environment, create a connection:
bq mk --connection --location=REGION --project_id=PROJECT_ID \ --connection_type=CLOUD_RESOURCE CONNECTION_ID
The
--project_idparameter overrides the default project.Replace the following:
REGION: your connection regionPROJECT_ID: your Google Cloud project IDCONNECTION_ID: an ID for your connection
When you create a connection resource, BigQuery creates a unique system service account and associates it with the connection.
Troubleshooting: If you get the following connection error, update the Google Cloud SDK:
Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
Retrieve and copy the service account ID for use in a later step:
bq show --connection PROJECT_ID.REGION.CONNECTION_ID
The output is similar to the following:
name properties 1234.REGION.CONNECTION_ID {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}
Terraform
Use the
google_bigquery_connection
resource.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following example creates a Cloud resource connection named
my_cloud_resource_connection in the US region:
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud Shell
- Launch Cloud Shell.
-
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Prepare the directory
Each Terraform configuration file must have its own directory (also called a root module).
-
In Cloud Shell, create a directory and a new
file within that directory. The filename must have the
.tfextension—for examplemain.tf. In this tutorial, the file is referred to asmain.tf.mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created
main.tf.Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
-
Initialize Terraform. You only need to do this once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgradeoption:terraform init -upgrade
Apply the changes
-
Review the configuration and verify that the resources that Terraform is going to create or
update match your expectations:
terraform plan
Make corrections to the configuration as necessary.
-
Apply the Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Wait until Terraform displays the "Apply complete!" message.
- Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
For more information, see Create and set up a Cloud resource connection.
Grant access to the service account
To run queries that use generative AI.* functions that call Vertex AI
models, you must grant appropriate permissions to the service account that was
created when you created your connection. To run functions that call a
Vertex AI foundation model, the Vertex AI User role
(roles/aiplatform.user) is required.
Select one of the following options:
Console
Go to the IAM & Admin page.
Click Grant access.
The Add principals dialog opens.
In the New principals field, enter the service account ID that you copied earlier.
In the Assign roles section, click Add roles.
Search for the Vertex AI User role, select it, and then click Apply.
Click Save.
gcloud
Use the gcloud projects add-iam-policy-binding command:
gcloud projects add-iam-policy-binding gs://PROJECT_ID \ --member="serviceAccount:$(bq show --format=prettyjson --connection $PROJECT_ID.$REGION.$CONNECTION_NAME | jq -r .cloudResource.serviceAccountId)" --role=roles/aiplatform.user
Replace the following:
PROJECT_ID: the name of your project.REGION: the location where the connection was created.CONNECTION_NAME: The name of the connection you created.
Terraform
Use the
google_bigquery_connection
resource.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following example grants IAM role access to the service account of the Cloud resource connection:
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud Shell
- Launch Cloud Shell.
-
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Prepare the directory
Each Terraform configuration file must have its own directory (also called a root module).
-
In Cloud Shell, create a directory and a new
file within that directory. The filename must have the
.tfextension—for examplemain.tf. In this tutorial, the file is referred to asmain.tf.mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created
main.tf.Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
-
Initialize Terraform. You only need to do this once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgradeoption:terraform init -upgrade
Apply the changes
-
Review the configuration and verify that the resources that Terraform is going to create or
update match your expectations:
terraform plan
Make corrections to the configuration as necessary.
-
Apply the Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Wait until Terraform displays the "Apply complete!" message.
- Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.