Skip to content

Commit a88e423

Browse files
feat: added support to install and start the Elastic's Natural Language Processing model using new input enable_elser_model<br>- the default value of the member_host_flavor in the DA has changed from multitenant to b3c.4x16.encrypted.<br>- the default value of the member_cpu_count in the DA has changed from 0 to 3<br>- the default value of plan in the DA has updated from enterprise to platinum. (#223)
1 parent 91599ec commit a88e423

File tree

16 files changed

+245
-31
lines changed

16 files changed

+245
-31
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ You need the following permissions to run this module.
6161
|------|---------|
6262
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
6363
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.65.0, <2.0.0 |
64+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1, < 4.0.0 |
6465
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.9.1 |
6566

6667
### Modules
@@ -77,6 +78,8 @@ You need the following permissions to run this module.
7778
| [ibm_iam_authorization_policy.policy](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
7879
| [ibm_resource_key.service_credentials](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_key) | resource |
7980
| [ibm_resource_tag.elasticsearch_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
81+
| [null_resource.put_vectordb_model](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
82+
| [null_resource.start_vectordb_model](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
8083
| [time_sleep.wait_for_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
8184
| [ibm_database_connection.database_connection](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/database_connection) | data source |
8285

@@ -91,6 +94,7 @@ You need the following permissions to run this module.
9194
| <a name="input_backup_encryption_key_crn"></a> [backup\_encryption\_key\_crn](#input\_backup\_encryption\_key\_crn) | The CRN of a KMS (Key Protect or Hyper Protect Crypto Service) key to use for encrypting the disk that holds deployment backups. Applies only if `kms_encryption_enabled` is true. Limitations exist for regions. For more information, see [Key Protect integration](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect&interface=ui#key-byok) or [Hyper Protect Crypto Services integration](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-hpcs#use-hpcs-backups). | `string` | `null` | no |
9295
| <a name="input_cbr_rules"></a> [cbr\_rules](#input\_cbr\_rules) | The list of context-based restriction rules to create. | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> }))</pre> | `[]` | no |
9396
| <a name="input_elasticsearch_version"></a> [elasticsearch\_version](#input\_elasticsearch\_version) | The version of Databases for Elasticsearch to deploy. Possible values: `8.7`, `8.10`, `8.12`, which requires an Enterprise Platinum pricing plan. If no value is specified, the current preferred version for IBM Cloud Databases is used. | `string` | `null` | no |
97+
| <a name="input_enable_elser_model"></a> [enable\_elser\_model](#input\_enable\_elser\_model) | Set it to true to install and start the Elastic's Natural Language Processing model. [Learn more](https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-elser-embeddings-elasticsearch) | `bool` | `false` | no |
9498
| <a name="input_existing_kms_instance_guid"></a> [existing\_kms\_instance\_guid](#input\_existing\_kms\_instance\_guid) | The GUID of a Hyper Protect Crypto Services or Key Protect instance for the CRN specified in `kms_key_crn` and `backup_encryption_key_crn`. Applies only if `kms_encryption_enabled` is true, `skip_iam_authorization_policy` is false, and you specify values for `kms_key_crn` or `backup_encryption_key_crn`. | `string` | `null` | no |
9599
| <a name="input_kms_encryption_enabled"></a> [kms\_encryption\_enabled](#input\_kms\_encryption\_enabled) | Whether to specify the keys used to encrypt data in the database. Specify `true` to identify the encryption keys. If set to `false`, the data is encrypted with randomly generated keys. [Learn more about Key Protect integration](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect). [Learn more about HPCS integration](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-hpcs). | `bool` | `false` | no |
96100
| <a name="input_kms_key_crn"></a> [kms\_key\_crn](#input\_kms\_key\_crn) | The root key CRN of the Key Protect or Hyper Protect Crypto Services instance to use for disk encryption. Applies only if `kms_encryption_enabled` is true. | `string` | `null` | no |

examples/fscloud/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module "elasticsearch" {
6868
member_host_flavor = "b3c.4x16.encrypted"
6969
backup_encryption_key_crn = var.backup_encryption_key_crn
7070
backup_crn = var.backup_crn
71+
enable_elser_model = var.enable_elser_model
7172
cbr_rules = [
7273
{
7374
description = "${var.prefix}-elasticsearch access only from vpc"

examples/fscloud/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,9 @@ variable "backup_encryption_key_crn" {
108108
default = null
109109
# Validation happens in the root module
110110
}
111+
112+
variable "enable_elser_model" {
113+
type = bool
114+
description = "Set it to true to install and start the Elastic's Natural Language Processing model. [Learn more](https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-elser-embeddings-elasticsearch)"
115+
default = false
116+
}

main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ locals {
88
validate_auth_policy = var.kms_encryption_enabled && var.skip_iam_authorization_policy == false && var.existing_kms_instance_guid == null ? tobool("When var.skip_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
99
# tflint-ignore: terraform_unused_declarations
1010
validate_backup_key = var.backup_encryption_key_crn != null && var.use_default_backup_encryption_key == true ? tobool("When passing a value for 'backup_encryption_key_crn' you cannot set 'use_default_backup_encryption_key' to 'true'") : true
11+
# tflint-ignore: terraform_unused_declarations
12+
validate_plan = var.enable_elser_model && var.plan != "platinum" ? tobool("When var.enable_elser_model is set to true, a value for var.plan must be 'platinum' in order to enable ELSER model.") : true
13+
# tflint-ignore: terraform_unused_declarations
14+
validate_es_user = var.enable_elser_model && !((length(var.service_credential_names) > 0 && length([for k, v in var.service_credential_names : k if v == "Administrator"]) > 0) || var.admin_pass != null) ? tobool("When var.enable_elser_model is set to true, a value must be passed for var.service_credential_names or var.admin_pass. var.service_credential_names must contain at least one credential name with Administrator role.") : true
1115

1216
# If no value passed for 'backup_encryption_key_crn' use the value of 'kms_key_crn' and perform validation of 'kms_key_crn' to check if region is supported by backup encryption key.
1317

@@ -262,3 +266,46 @@ data "ibm_database_connection" "database_connection" {
262266
user_id = ibm_database.elasticsearch.adminuser
263267
user_type = "database"
264268
}
269+
270+
##############################################################################
271+
# ELSER support
272+
##############################################################################
273+
274+
# Enable Elastic's Natural Language Processing model (ELSER) support by calling ES REST API directly using shell script. Learn more https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-elser-embeddings-elasticsearch
275+
# Firstly, ELSER model is installed using 'put_vectordb_model' null_resource. Secondly, ELSER model is started with `start_vectordb_model` null_resource.
276+
#
277+
# To authenticate ES rest API, the credentials are extracted from 'service_credential_names' or ES 'adminpassword' using the following logic:
278+
# if elser_model is enabled, then
279+
# if service_credential_names are used, then get the key name of a credential where role is 'Administrator'
280+
# use the key name to obtain username and password from service_credentials_object
281+
# else if admin_pass is used, then use 'admin' for username and password from ES password
282+
locals {
283+
es_admin_users = var.enable_elser_model && var.service_credential_names != null && length(var.service_credential_names) > 0 ? [for k, v in var.service_credential_names : k if v == "Administrator"] : []
284+
es_admin_user = length(local.es_admin_users) > 0 ? local.es_admin_users[0] : null
285+
es_username = local.es_admin_user != null ? local.service_credentials_object["credentials"][local.es_admin_user]["username"] : var.admin_pass != null ? "admin" : null
286+
es_password = local.es_admin_user != null ? local.service_credentials_object["credentials"][local.es_admin_user]["password"] : var.admin_pass != null ? ibm_database.elasticsearch.adminpassword : null
287+
es_url = local.es_username != null && local.es_password != null ? "https://${local.es_username}:${local.es_password}@${data.ibm_database_connection.database_connection.https[0].hosts[0].hostname}:${data.ibm_database_connection.database_connection.https[0].hosts[0].port}" : null
288+
}
289+
290+
resource "null_resource" "put_vectordb_model" {
291+
count = var.enable_elser_model ? 1 : 0
292+
provisioner "local-exec" {
293+
command = "${path.module}/scripts/put_vectordb_model.sh"
294+
interpreter = ["/bin/bash", "-c"]
295+
environment = {
296+
ES = local.es_url
297+
}
298+
}
299+
}
300+
301+
resource "null_resource" "start_vectordb_model" {
302+
depends_on = [null_resource.put_vectordb_model]
303+
count = var.enable_elser_model ? 1 : 0
304+
provisioner "local-exec" {
305+
command = "${path.module}/scripts/start_vectordb_model.sh"
306+
interpreter = ["/bin/bash", "-c"]
307+
environment = {
308+
ES = local.es_url
309+
}
310+
}
311+
}

modules/fscloud/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ No resources.
3737
| <a name="input_backup_encryption_key_crn"></a> [backup\_encryption\_key\_crn](#input\_backup\_encryption\_key\_crn) | The CRN of a Hyper Protect Crypto Service use for encrypting the disk that holds deployment backups. Only used if var.kms\_encryption\_enabled is set to true. There are limitation per region on the Hyper Protect Crypto Services and region for those services. See https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-hpcs#use-hpcs-backups | `string` | `null` | no |
3838
| <a name="input_cbr_rules"></a> [cbr\_rules](#input\_cbr\_rules) | (Optional, list) List of CBR rules to create | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> }))</pre> | `[]` | no |
3939
| <a name="input_elasticsearch_version"></a> [elasticsearch\_version](#input\_elasticsearch\_version) | Version of the Elasticsearch instance. If no value is passed, the current preferred version of IBM Cloud Databases is used. | `string` | `null` | no |
40+
| <a name="input_enable_elser_model"></a> [enable\_elser\_model](#input\_enable\_elser\_model) | Set it to true to install and start the Elastic's Natural Language Processing model.[Learn more](https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-elser-embeddings-elasticsearch) | `bool` | `false` | no |
4041
| <a name="input_existing_kms_instance_guid"></a> [existing\_kms\_instance\_guid](#input\_existing\_kms\_instance\_guid) | The GUID of the Hyper Protect Crypto Services instance. It is only required while creating authorization policy. | `string` | `null` | no |
4142
| <a name="input_kms_key_crn"></a> [kms\_key\_crn](#input\_kms\_key\_crn) | The root key CRN of the Hyper Protect Crypto Service (HPCS) to use for disk encryption. | `string` | n/a | yes |
4243
| <a name="input_member_cpu_count"></a> [member\_cpu\_count](#input\_member\_cpu\_count) | Allocated dedicated CPU per member. For shared CPU, set to 0. For more information, see https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-resources-scaling | `number` | `0` | no |

modules/fscloud/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ module "elasticsearch" {
2424
member_host_flavor = var.member_host_flavor
2525
auto_scaling = var.auto_scaling
2626
service_credential_names = var.service_credential_names
27+
enable_elser_model = var.enable_elser_model
2728
}

modules/fscloud/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,9 @@ variable "cbr_rules" {
190190
default = []
191191
# Validation happens in the rule module
192192
}
193+
194+
variable "enable_elser_model" {
195+
type = bool
196+
description = "Set it to true to install and start the Elastic's Natural Language Processing model.[Learn more](https://cloud.ibm.com/docs/databases-for-elasticsearch?topic=databases-for-elasticsearch-elser-embeddings-elasticsearch)"
197+
default = false
198+
}

scripts/put_vectordb_model.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -e
3+
4+
#Function to install the vectorDB model
5+
Put_model() {
6+
7+
sleep=2
8+
for i in $(seq 1 4); do
9+
sleep=$((sleep*2))
10+
11+
sleep $sleep
12+
# learn more https://www.elastic.co/docs/api/doc/elasticsearch-serverless/operation/operation-ml-put-trained-model#operation-ml-put-trained-model-wait_for_completion
13+
response=$(curl -s -w "%{http_code}" -kX PUT "$ES/_ml/trained_models/.elser_model_1?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d'
14+
{
15+
"input": {
16+
"field_names": ["text_field"]
17+
}
18+
}
19+
')
20+
21+
http_code=$(tail -n1 <<< "$response")
22+
content=$(sed '$ d' <<< "$response")
23+
24+
# Check the HTTP status code
25+
if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
26+
echo "Request sent successfully."
27+
break
28+
else
29+
echo "Failed to install the vectorDB model. HTTP status code: $http_code"
30+
echo "Reponse: $content"
31+
if [ "$i" -eq 4 ]; then
32+
exit 1
33+
fi
34+
fi
35+
done
36+
37+
}
38+
39+
Put_model

scripts/start_vectordb_model.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -e
3+
4+
#Function to start the vectorDB model
5+
Start_model() {
6+
7+
# It takes few minute for the model to finish installing before we can start trained model deployment, therefore we sleep for 180 seconds (3m).
8+
sleep 180
9+
# Learn more https://www.elastic.co/guide/en/elasticsearch/reference/current/start-trained-model-deployment.html
10+
response=$(curl -s -w "%{http_code}" -kX POST "$ES/_ml/trained_models/.elser_model_1/deployment/_start?wait_for=started&timeout=3m&deployment_id=for_search&pretty")
11+
12+
http_code=$(tail -n1 <<< "$response")
13+
content=$(sed '$ d' <<< "$response")
14+
15+
# Check the HTTP status code
16+
if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
17+
echo "Request sent successfully."
18+
else
19+
echo "Failed to start the vectorDB model. HTTP status code: $http_code"
20+
echo "Reponse: $content"
21+
exit 1
22+
fi
23+
24+
}
25+
26+
Start_model

solutions/standard/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ module "elasticsearch" {
8383
member_cpu_count = var.member_cpu_count
8484
auto_scaling = var.auto_scaling
8585
service_credential_names = var.service_credential_names
86+
enable_elser_model = var.enable_elser_model
8687
}

0 commit comments

Comments
 (0)