Skip to content

Commit 5606fb9

Browse files
vertex_ai: add google_vertex_ai_index_endpoint_deployed_index resource (#11039) (#764)
[upstream:818c46b4491a3476fa383e5c75fe1dfd2bb304bc] Signed-off-by: Modular Magician <[email protected]>
1 parent cedc1ba commit 5606fb9

File tree

16 files changed

+697
-0
lines changed

16 files changed

+697
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
resource "google_vertex_ai_index_endpoint_deployed_index" "automatic_resources" {
2+
depends_on = [ google_vertex_ai_index_endpoint.vertex_endpoint ]
3+
index_endpoint = google_vertex_ai_index_endpoint.vertex_endpoint.id
4+
index = google_vertex_ai_index.index.id // this is the index that will be deployed onto an endpoint
5+
deployed_index_id = "deployed_index_id-${local.name_suffix}"
6+
display_name = "vertex-deployed-index-${local.name_suffix}"
7+
automatic_resources{
8+
max_replica_count = 2
9+
min_replica_count = 1
10+
}
11+
deployment_group = "test"
12+
}
13+
14+
resource "google_storage_bucket" "bucket" {
15+
name = "bucket-name-${local.name_suffix}"
16+
location = "us-central1"
17+
uniform_bucket_level_access = true
18+
}
19+
20+
# The sample data comes from the following link:
21+
# https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#specify-namespaces-tokens
22+
resource "google_storage_bucket_object" "data" {
23+
name = "contents/data.json"
24+
bucket = google_storage_bucket.bucket.name
25+
content = <<EOF
26+
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
27+
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
28+
EOF
29+
}
30+
31+
resource "google_vertex_ai_index" "index" {
32+
labels = {
33+
foo = "bar"
34+
}
35+
region = "us-central1"
36+
display_name = "test-index-${local.name_suffix}"
37+
description = "index for test"
38+
metadata {
39+
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
40+
config {
41+
dimensions = 2
42+
approximate_neighbors_count = 150
43+
shard_size = "SHARD_SIZE_MEDIUM"
44+
distance_measure_type = "DOT_PRODUCT_DISTANCE"
45+
algorithm_config {
46+
tree_ah_config {
47+
leaf_node_embedding_count = 500
48+
leaf_nodes_to_search_percent = 7
49+
}
50+
}
51+
}
52+
}
53+
index_update_method = "BATCH_UPDATE"
54+
}
55+
56+
57+
resource "google_vertex_ai_index_endpoint" "vertex_endpoint" {
58+
display_name = "sample-endpoint"
59+
description = "A sample vertex endpoint"
60+
region = "us-central1"
61+
labels = {
62+
label-one = "value-one"
63+
}
64+
public_endpoint_enabled = true
65+
}
66+
67+
data "google_project" "project" {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Vertex Ai Index Endpoint Deployed Index Automatic Resources - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="[email protected]" analyticsId="UA-125550242-1" tutorialName="vertex_ai_index_endpoint_deployed_index_automatic_resources" repositoryUrl="https://github.com/terraform-google-modules/docs-examples"></walkthrough-author>
6+
7+
Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform.
8+
9+
<walkthrough-project-billing-setup></walkthrough-project-billing-setup>
10+
11+
Terraform provisions real GCP resources, so anything you create in this session will be billed against this project.
12+
13+
## Terraforming!
14+
15+
Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command
16+
to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up
17+
the project name from the environment variable.
18+
19+
```bash
20+
export GOOGLE_CLOUD_PROJECT={{project-id}}
21+
```
22+
23+
After that, let's get Terraform started. Run the following to pull in the providers.
24+
25+
```bash
26+
terraform init
27+
```
28+
29+
With the providers downloaded and a project set, you're ready to use Terraform. Go ahead!
30+
31+
```bash
32+
terraform apply
33+
```
34+
35+
Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan.
36+
37+
```bash
38+
yes
39+
```
40+
41+
42+
## Post-Apply
43+
44+
### Editing your config
45+
46+
Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed.
47+
48+
```bash
49+
terraform plan
50+
```
51+
52+
So let's make a change! Try editing a number, or appending a value to the name in the editor. Then,
53+
run a 'plan' again.
54+
55+
```bash
56+
terraform plan
57+
```
58+
59+
Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes
60+
at the 'yes' prompt.
61+
62+
```bash
63+
terraform apply
64+
```
65+
66+
```bash
67+
yes
68+
```
69+
70+
## Cleanup
71+
72+
Run the following to remove the resources Terraform provisioned:
73+
74+
```bash
75+
terraform destroy
76+
```
77+
```bash
78+
yes
79+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
resource "google_service_account" "sa" {
2+
account_id = "vertex-sa-${local.name_suffix}"
3+
}
4+
5+
resource "google_vertex_ai_index_endpoint_deployed_index" "basic_deployed_index" {
6+
depends_on = [ google_vertex_ai_index_endpoint.vertex_index_endpoint_deployed, google_service_account.sa ]
7+
index_endpoint = google_vertex_ai_index_endpoint.vertex_index_endpoint_deployed.id
8+
index = google_vertex_ai_index.index.id // this is the index that will be deployed onto an endpoint
9+
deployed_index_id = "deployed_index_id-${local.name_suffix}"
10+
reserved_ip_ranges = ["vertex-ai-range-${local.name_suffix}"]
11+
enable_access_logging = false
12+
display_name = "vertex-deployed-index-${local.name_suffix}"
13+
deployed_index_auth_config{
14+
auth_provider{
15+
audiences = ["123456-my-app"]
16+
allowed_issuers = ["${google_service_account.sa.email}"]
17+
}
18+
}
19+
}
20+
21+
resource "google_storage_bucket" "bucket" {
22+
name = "bucket-name-${local.name_suffix}"
23+
location = "us-central1"
24+
uniform_bucket_level_access = true
25+
}
26+
27+
# The sample data comes from the following link:
28+
# https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#specify-namespaces-tokens
29+
resource "google_storage_bucket_object" "data" {
30+
name = "contents/data.json"
31+
bucket = google_storage_bucket.bucket.name
32+
content = <<EOF
33+
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]}
34+
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]}
35+
EOF
36+
}
37+
38+
resource "google_vertex_ai_index" "index" {
39+
labels = {
40+
foo = "bar"
41+
}
42+
region = "us-central1"
43+
display_name = "test-index-${local.name_suffix}"
44+
description = "index for test"
45+
metadata {
46+
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents"
47+
config {
48+
dimensions = 2
49+
approximate_neighbors_count = 150
50+
shard_size = "SHARD_SIZE_SMALL"
51+
distance_measure_type = "DOT_PRODUCT_DISTANCE"
52+
algorithm_config {
53+
tree_ah_config {
54+
leaf_node_embedding_count = 500
55+
leaf_nodes_to_search_percent = 7
56+
}
57+
}
58+
}
59+
}
60+
index_update_method = "BATCH_UPDATE"
61+
}
62+
63+
resource "google_vertex_ai_index_endpoint" "vertex_index_endpoint_deployed" {
64+
display_name = "sample-endpoint"
65+
description = "A sample vertex endpoint"
66+
region = "us-central1"
67+
labels = {
68+
label-one = "value-one"
69+
}
70+
network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.vertex_network.name}"
71+
}
72+
73+
data "google_compute_network" "vertex_network" {
74+
name = "network-name-${local.name_suffix}"
75+
}
76+
77+
data "google_project" "project" {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Vertex Ai Index Endpoint Deployed Index Basic - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="[email protected]" analyticsId="UA-125550242-1" tutorialName="vertex_ai_index_endpoint_deployed_index_basic" repositoryUrl="https://github.com/terraform-google-modules/docs-examples"></walkthrough-author>
6+
7+
Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform.
8+
9+
<walkthrough-project-billing-setup></walkthrough-project-billing-setup>
10+
11+
Terraform provisions real GCP resources, so anything you create in this session will be billed against this project.
12+
13+
## Terraforming!
14+
15+
Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command
16+
to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up
17+
the project name from the environment variable.
18+
19+
```bash
20+
export GOOGLE_CLOUD_PROJECT={{project-id}}
21+
```
22+
23+
After that, let's get Terraform started. Run the following to pull in the providers.
24+
25+
```bash
26+
terraform init
27+
```
28+
29+
With the providers downloaded and a project set, you're ready to use Terraform. Go ahead!
30+
31+
```bash
32+
terraform apply
33+
```
34+
35+
Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan.
36+
37+
```bash
38+
yes
39+
```
40+
41+
42+
## Post-Apply
43+
44+
### Editing your config
45+
46+
Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed.
47+
48+
```bash
49+
terraform plan
50+
```
51+
52+
So let's make a change! Try editing a number, or appending a value to the name in the editor. Then,
53+
run a 'plan' again.
54+
55+
```bash
56+
terraform plan
57+
```
58+
59+
Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes
60+
at the 'yes' prompt.
61+
62+
```bash
63+
terraform apply
64+
```
65+
66+
```bash
67+
yes
68+
```
69+
70+
## Cleanup
71+
72+
Run the following to remove the resources Terraform provisioned:
73+
74+
```bash
75+
terraform destroy
76+
```
77+
```bash
78+
yes
79+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}

0 commit comments

Comments
 (0)