Skip to content

Commit

Permalink
feat(vector search): Add examples for mutate, PSC, and VPC (#755)
Browse files Browse the repository at this point in the history
* feat(vector search): Add examples for mutate, PSC, and VPC

* set PSC allowlist project to name instead of number

* Allow destruction of google_service_networking_connection when residual resources still exist

See hashicorp/terraform-provider-google#18729 and
b/308248337#comment7 for more context

* Make deployed_index_ids unique

* Fix PSC allowlist project ids

---------

Co-authored-by: Katie McLaughlin <[email protected]>
  • Loading branch information
ericgribkoff and glasnt authored Dec 10, 2024
1 parent 47a39d8 commit 6aaaf12
Show file tree
Hide file tree
Showing 7 changed files with 711 additions and 0 deletions.
95 changes: 95 additions & 0 deletions vertex_ai/index_endpoint_deploy_psc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


# [START aiplatform_deploy_index_endpoint_psc_sample]
provider "google" {
region = "us-central1"
}

resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
depends_on = [google_vertex_ai_index_endpoint.default]
index_endpoint = google_vertex_ai_index_endpoint.default.id
index = google_vertex_ai_index.default.id
deployed_index_id = "deployed_index_for_psc"
}

resource "google_vertex_ai_index_endpoint" "default" {
display_name = "sample-endpoint"
description = "A sample index endpoint with Private Service Connect enabled"
region = "us-central1"
private_service_connect_config {
enable_private_service_connect = true
project_allowlist = [
data.google_project.project.project_id,
]
}
}

data "google_project" "project" {}

# Cloud Storage bucket name must be unique
resource "random_id" "default" {
byte_length = 8
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "bucket" {
name = "vertex-ai-index-bucket-${random_id.default.hex}"
location = "us-central1"
uniform_bucket_level_access = true
}

# Create index content
resource "google_storage_bucket_object" "data" {
name = "contents/data.json"
bucket = google_storage_bucket.bucket.name
content = <<EOF
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
EOF
}

resource "google_vertex_ai_index" "default" {
region = "us-central1"
display_name = "sample-index-batch-update"
description = "A sample index for batch update"
labels = {
foo = "bar"
}

metadata {
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
config {
dimensions = 2
approximate_neighbors_count = 150
distance_measure_type = "DOT_PRODUCT_DISTANCE"
algorithm_config {
tree_ah_config {
leaf_node_embedding_count = 500
leaf_nodes_to_search_percent = 7
}
}
}
}
index_update_method = "BATCH_UPDATE"

timeouts {
create = "2h"
update = "1h"
}
}
# [END aiplatform_deploy_index_endpoint_psc_sample]
113 changes: 113 additions & 0 deletions vertex_ai/index_endpoint_deploy_vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


# [START aiplatform_deploy_index_endpoint_vpc_sample]
provider "google" {
region = "us-central1"
}

resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
depends_on = [google_vertex_ai_index_endpoint.default]
index_endpoint = google_vertex_ai_index_endpoint.default.id
index = google_vertex_ai_index.default.id
deployed_index_id = "deployed_index_for_vpc"
}

resource "google_vertex_ai_index_endpoint" "default" {
display_name = "sample-endpoint"
description = "A sample index endpoint within a VPC network"
region = "us-central1"
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
depends_on = [
google_service_networking_connection.default
]
}

resource "google_service_networking_connection" "default" {
network = google_compute_network.default.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.default.name]
# Workaround to allow `terraform destroy`, see https://github.com/hashicorp/terraform-provider-google/issues/18729
deletion_policy = "ABANDON"
}

resource "google_compute_global_address" "default" {
name = "sample-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.default.id
}

resource "google_compute_network" "default" {
name = "sample-network"
}

data "google_project" "project" {}

# Cloud Storage bucket name must be unique
resource "random_id" "default" {
byte_length = 8
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "bucket" {
name = "vertex-ai-index-bucket-${random_id.default.hex}"
location = "us-central1"
uniform_bucket_level_access = true
}

# Create index content
resource "google_storage_bucket_object" "data" {
name = "contents/data.json"
bucket = google_storage_bucket.bucket.name
content = <<EOF
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
EOF
}

resource "google_vertex_ai_index" "default" {
region = "us-central1"
display_name = "sample-index-batch-update"
description = "A sample index for batch update"
labels = {
foo = "bar"
}

metadata {
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
config {
dimensions = 2
approximate_neighbors_count = 150
distance_measure_type = "DOT_PRODUCT_DISTANCE"
algorithm_config {
tree_ah_config {
leaf_node_embedding_count = 500
leaf_nodes_to_search_percent = 7
}
}
}
}
index_update_method = "BATCH_UPDATE"

timeouts {
create = "2h"
update = "1h"
}
}
# [END aiplatform_deploy_index_endpoint_vpc_sample]
95 changes: 95 additions & 0 deletions vertex_ai/index_endpoint_mutate/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


# [START aiplatform_mutate_index_endpoint_sample]
provider "google" {
region = "us-central1"
}

resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
depends_on = [google_vertex_ai_index_endpoint.default]
index_endpoint = google_vertex_ai_index_endpoint.default.id
index = google_vertex_ai_index.default.id
deployed_index_id = "deployed_index_for_mutate"
# This example assumes the deployed index endpoint's resources configuration
# differs from the values specified below. Terraform will mutate the deployed
# index endpoint's resource configuration to match.
automatic_resources {
min_replica_count = 3
max_replica_count = 5
}
}

resource "google_vertex_ai_index_endpoint" "default" {
display_name = "sample-endpoint"
description = "A sample index endpoint with a public endpoint"
region = "us-central1"
public_endpoint_enabled = true
}

# Cloud Storage bucket name must be unique
resource "random_id" "default" {
byte_length = 8
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "bucket" {
name = "vertex-ai-index-bucket-${random_id.default.hex}"
location = "us-central1"
uniform_bucket_level_access = true
}

# Create index content
resource "google_storage_bucket_object" "data" {
name = "contents/data.json"
bucket = google_storage_bucket.bucket.name
content = <<EOF
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
EOF
}

resource "google_vertex_ai_index" "default" {
region = "us-central1"
display_name = "sample-index-batch-update"
description = "A sample index for batch update"
labels = {
foo = "bar"
}

metadata {
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
config {
dimensions = 2
approximate_neighbors_count = 150
distance_measure_type = "DOT_PRODUCT_DISTANCE"
algorithm_config {
tree_ah_config {
leaf_node_embedding_count = 500
leaf_nodes_to_search_percent = 7
}
}
}
}
index_update_method = "BATCH_UPDATE"

timeouts {
create = "2h"
update = "1h"
}
}
# [END aiplatform_mutate_index_endpoint_sample]
Loading

0 comments on commit 6aaaf12

Please sign in to comment.