Skip to content

Commit 6833acf

Browse files
ericgribkoffglasnt
authored andcommitted
feat(vector search): Add examples for mutate, PSC, and VPC (terraform-google-modules#755)
* 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]>
1 parent 54c6035 commit 6833acf

File tree

7 files changed

+711
-0
lines changed

7 files changed

+711
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
# [START aiplatform_deploy_index_endpoint_psc_sample]
19+
provider "google" {
20+
region = "us-central1"
21+
}
22+
23+
resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
24+
depends_on = [google_vertex_ai_index_endpoint.default]
25+
index_endpoint = google_vertex_ai_index_endpoint.default.id
26+
index = google_vertex_ai_index.default.id
27+
deployed_index_id = "deployed_index_for_psc"
28+
}
29+
30+
resource "google_vertex_ai_index_endpoint" "default" {
31+
display_name = "sample-endpoint"
32+
description = "A sample index endpoint with Private Service Connect enabled"
33+
region = "us-central1"
34+
private_service_connect_config {
35+
enable_private_service_connect = true
36+
project_allowlist = [
37+
data.google_project.project.project_id,
38+
]
39+
}
40+
}
41+
42+
data "google_project" "project" {}
43+
44+
# Cloud Storage bucket name must be unique
45+
resource "random_id" "default" {
46+
byte_length = 8
47+
}
48+
49+
# Create a Cloud Storage bucket
50+
resource "google_storage_bucket" "bucket" {
51+
name = "vertex-ai-index-bucket-${random_id.default.hex}"
52+
location = "us-central1"
53+
uniform_bucket_level_access = true
54+
}
55+
56+
# Create index content
57+
resource "google_storage_bucket_object" "data" {
58+
name = "contents/data.json"
59+
bucket = google_storage_bucket.bucket.name
60+
content = <<EOF
61+
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
62+
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
63+
EOF
64+
}
65+
66+
resource "google_vertex_ai_index" "default" {
67+
region = "us-central1"
68+
display_name = "sample-index-batch-update"
69+
description = "A sample index for batch update"
70+
labels = {
71+
foo = "bar"
72+
}
73+
74+
metadata {
75+
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
76+
config {
77+
dimensions = 2
78+
approximate_neighbors_count = 150
79+
distance_measure_type = "DOT_PRODUCT_DISTANCE"
80+
algorithm_config {
81+
tree_ah_config {
82+
leaf_node_embedding_count = 500
83+
leaf_nodes_to_search_percent = 7
84+
}
85+
}
86+
}
87+
}
88+
index_update_method = "BATCH_UPDATE"
89+
90+
timeouts {
91+
create = "2h"
92+
update = "1h"
93+
}
94+
}
95+
# [END aiplatform_deploy_index_endpoint_psc_sample]
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
# [START aiplatform_deploy_index_endpoint_vpc_sample]
19+
provider "google" {
20+
region = "us-central1"
21+
}
22+
23+
resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
24+
depends_on = [google_vertex_ai_index_endpoint.default]
25+
index_endpoint = google_vertex_ai_index_endpoint.default.id
26+
index = google_vertex_ai_index.default.id
27+
deployed_index_id = "deployed_index_for_vpc"
28+
}
29+
30+
resource "google_vertex_ai_index_endpoint" "default" {
31+
display_name = "sample-endpoint"
32+
description = "A sample index endpoint within a VPC network"
33+
region = "us-central1"
34+
network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.default.name}"
35+
depends_on = [
36+
google_service_networking_connection.default
37+
]
38+
}
39+
40+
resource "google_service_networking_connection" "default" {
41+
network = google_compute_network.default.id
42+
service = "servicenetworking.googleapis.com"
43+
reserved_peering_ranges = [google_compute_global_address.default.name]
44+
# Workaround to allow `terraform destroy`, see https://github.com/hashicorp/terraform-provider-google/issues/18729
45+
deletion_policy = "ABANDON"
46+
}
47+
48+
resource "google_compute_global_address" "default" {
49+
name = "sample-address"
50+
purpose = "VPC_PEERING"
51+
address_type = "INTERNAL"
52+
prefix_length = 16
53+
network = google_compute_network.default.id
54+
}
55+
56+
resource "google_compute_network" "default" {
57+
name = "sample-network"
58+
}
59+
60+
data "google_project" "project" {}
61+
62+
# Cloud Storage bucket name must be unique
63+
resource "random_id" "default" {
64+
byte_length = 8
65+
}
66+
67+
# Create a Cloud Storage bucket
68+
resource "google_storage_bucket" "bucket" {
69+
name = "vertex-ai-index-bucket-${random_id.default.hex}"
70+
location = "us-central1"
71+
uniform_bucket_level_access = true
72+
}
73+
74+
# Create index content
75+
resource "google_storage_bucket_object" "data" {
76+
name = "contents/data.json"
77+
bucket = google_storage_bucket.bucket.name
78+
content = <<EOF
79+
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
80+
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
81+
EOF
82+
}
83+
84+
resource "google_vertex_ai_index" "default" {
85+
region = "us-central1"
86+
display_name = "sample-index-batch-update"
87+
description = "A sample index for batch update"
88+
labels = {
89+
foo = "bar"
90+
}
91+
92+
metadata {
93+
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
94+
config {
95+
dimensions = 2
96+
approximate_neighbors_count = 150
97+
distance_measure_type = "DOT_PRODUCT_DISTANCE"
98+
algorithm_config {
99+
tree_ah_config {
100+
leaf_node_embedding_count = 500
101+
leaf_nodes_to_search_percent = 7
102+
}
103+
}
104+
}
105+
}
106+
index_update_method = "BATCH_UPDATE"
107+
108+
timeouts {
109+
create = "2h"
110+
update = "1h"
111+
}
112+
}
113+
# [END aiplatform_deploy_index_endpoint_vpc_sample]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
# [START aiplatform_mutate_index_endpoint_sample]
19+
provider "google" {
20+
region = "us-central1"
21+
}
22+
23+
resource "google_vertex_ai_index_endpoint_deployed_index" "default" {
24+
depends_on = [google_vertex_ai_index_endpoint.default]
25+
index_endpoint = google_vertex_ai_index_endpoint.default.id
26+
index = google_vertex_ai_index.default.id
27+
deployed_index_id = "deployed_index_for_mutate"
28+
# This example assumes the deployed index endpoint's resources configuration
29+
# differs from the values specified below. Terraform will mutate the deployed
30+
# index endpoint's resource configuration to match.
31+
automatic_resources {
32+
min_replica_count = 3
33+
max_replica_count = 5
34+
}
35+
}
36+
37+
resource "google_vertex_ai_index_endpoint" "default" {
38+
display_name = "sample-endpoint"
39+
description = "A sample index endpoint with a public endpoint"
40+
region = "us-central1"
41+
public_endpoint_enabled = true
42+
}
43+
44+
# Cloud Storage bucket name must be unique
45+
resource "random_id" "default" {
46+
byte_length = 8
47+
}
48+
49+
# Create a Cloud Storage bucket
50+
resource "google_storage_bucket" "bucket" {
51+
name = "vertex-ai-index-bucket-${random_id.default.hex}"
52+
location = "us-central1"
53+
uniform_bucket_level_access = true
54+
}
55+
56+
# Create index content
57+
resource "google_storage_bucket_object" "data" {
58+
name = "contents/data.json"
59+
bucket = google_storage_bucket.bucket.name
60+
content = <<EOF
61+
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
62+
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
63+
EOF
64+
}
65+
66+
resource "google_vertex_ai_index" "default" {
67+
region = "us-central1"
68+
display_name = "sample-index-batch-update"
69+
description = "A sample index for batch update"
70+
labels = {
71+
foo = "bar"
72+
}
73+
74+
metadata {
75+
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
76+
config {
77+
dimensions = 2
78+
approximate_neighbors_count = 150
79+
distance_measure_type = "DOT_PRODUCT_DISTANCE"
80+
algorithm_config {
81+
tree_ah_config {
82+
leaf_node_embedding_count = 500
83+
leaf_nodes_to_search_percent = 7
84+
}
85+
}
86+
}
87+
}
88+
index_update_method = "BATCH_UPDATE"
89+
90+
timeouts {
91+
create = "2h"
92+
update = "1h"
93+
}
94+
}
95+
# [END aiplatform_mutate_index_endpoint_sample]

0 commit comments

Comments
 (0)