-
Notifications
You must be signed in to change notification settings - Fork 1
/
opensearch.tf
142 lines (133 loc) · 4.05 KB
/
opensearch.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# – OpenSearch Serverless Default –
# Create a Collection
resource "awscc_opensearchserverless_collection" "default_collection" {
count = var.create_default_kb ? 1 : 0
name = "default-collection-${random_string.solution_prefix.result}"
type = "VECTORSEARCH"
description = "Default collection created by Amazon Bedrock Knowledge base."
depends_on = [
aws_opensearchserverless_security_policy.security_policy[0],
aws_opensearchserverless_security_policy.nw_policy[0]
]
}
# Encryption Security Policy
resource "aws_opensearchserverless_security_policy" "security_policy" {
count = var.create_default_kb ? 1 : 0
name = "awscc-security-policy-${random_string.solution_prefix.result}"
type = "encryption"
policy = jsonencode({
Rules = [
{
Resource = ["collection/default-collection-${random_string.solution_prefix.result}"]
ResourceType = "collection"
}
],
AWSOwnedKey = true
})
}
# Network policy
resource "aws_opensearchserverless_security_policy" "nw_policy" {
count = var.create_default_kb ? 1 : 0
name = "nw-policy-${random_string.solution_prefix.result}"
type = "network"
policy = jsonencode([
{
Rules = [
{
ResourceType = "collection"
Resource = ["collection/default-collection-${random_string.solution_prefix.result}"]
},
]
AllowFromPublic = true,
},
{
Description = "Public access for dashboards",
Rules = [
{
ResourceType = "dashboard"
Resource = [
"collection/default-collection-${random_string.solution_prefix.result}"
]
}
],
AllowFromPublic = true
}
])
}
# Data policy
resource "aws_opensearchserverless_access_policy" "data_policy" {
count = var.create_default_kb ? 1 : 0
name = "os-access-policy-${random_string.solution_prefix.result}"
type = "data"
policy = jsonencode([
{
Rules = [
{
ResourceType = "index"
Resource = [
"index/${awscc_opensearchserverless_collection.default_collection[0].name}/*"
]
Permission = [
"aoss:*"
]
},
{
ResourceType = "collection"
Resource = [
"collection/${awscc_opensearchserverless_collection.default_collection[0].name}"
]
Permission = [
"aoss:*"
]
}
],
Principal = [
var.kb_role_arn != null ? var.kb_role_arn : aws_iam_role.bedrock_knowledge_base_role[0].arn,
data.aws_caller_identity.current.arn
]
}
])
}
# OpenSearch index
resource "time_sleep" "wait_before_index_creation" {
count = var.create_default_kb ? 1 : 0
depends_on = [aws_opensearchserverless_access_policy.data_policy[0]]
create_duration = "60s" # Wait for 60 seconds before creating the index
}
resource "opensearch_index" "default_oss_index" {
count = var.create_default_kb ? 1 : 0
name = "bedrock-knowledge-base-default-index-${random_string.solution_prefix.result}"
number_of_shards = "2"
number_of_replicas = "0"
index_knn = true
index_knn_algo_param_ef_search = "512"
mappings = <<-EOF
{
"properties": {
"bedrock-knowledge-base-default-vector": {
"type": "knn_vector",
"dimension": 1536,
"method": {
"name": "hnsw",
"engine": "faiss",
"parameters": {
"m": 16,
"ef_construction": 512
},
"space_type": "l2"
}
},
"AMAZON_BEDROCK_METADATA": {
"type": "text",
"index": "false"
},
"AMAZON_BEDROCK_TEXT_CHUNK": {
"type": "text",
"index": "true"
}
}
}
EOF
force_destroy = true
depends_on = [time_sleep.wait_before_index_creation, aws_opensearchserverless_access_policy.data_policy[0]]
}