-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_database.tf
145 lines (123 loc) · 4.08 KB
/
vector_database.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
143
144
145
################################################################################
# PostgreSQL Serverless v2
################################################################################
resource "aws_iam_role" "vector_store_cluster_role" {
name = "genai-cluster-role-${random_pet.this.id}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "rds.amazonaws.com"
}
},
]
})
tags = var.aws_resource_tags
}
resource "aws_iam_role_policy" "vector_store_cluster_role_policy" {
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"bedrock:InvokeModel",
]
Effect = "Allow"
Resource = [
"arn:aws:bedrock:*:${data.aws_caller_identity.current.account_id}:provisioned-model/*",
"arn:aws:bedrock:*::foundation-model/*"
]
},
]
})
role = aws_iam_role.vector_store_cluster_role.id
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.15.0"
name = "genai-vpc-${random_pet.this.id}"
cidr = local.vpc_cidr
azs = local.azs
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 3)]
database_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 6)]
tags = var.aws_resource_tags
}
module "aurora_postgresql_v2" {
source = "terraform-aws-modules/rds-aurora/aws"
version = "9.10.0"
name = "genai-bedrock-vector-store-${random_pet.this.id}"
engine = var.rds_engine
engine_mode = "provisioned"
engine_version = var.rds_engine_version
storage_encrypted = true
master_username = var.rds_master_username
vpc_id = module.vpc.vpc_id
db_subnet_group_name = module.vpc.database_subnet_group_name
security_group_rules = {
vpc_ingress = {
cidr_blocks = module.vpc.private_subnets_cidr_blocks
}
}
apply_immediately = true
skip_final_snapshot = true
enable_http_endpoint = true
serverlessv2_scaling_configuration = {
min_capacity = 2
max_capacity = 10
}
instance_class = "db.serverless"
instances = {
one = {}
}
iam_roles = {
rds_cluster_role = {
feature_name = "SageMaker"
role_arn = aws_iam_role.vector_store_cluster_role.arn
}
}
tags = var.aws_resource_tags
}
module "vector_store_bedrock_secret" {
source = "terraform-aws-modules/secrets-manager/aws"
version = "1.3.1"
# Secret
name_prefix = "bedrock-secret-${random_pet.this.id}"
description = "Vector Store bedrock secret"
recovery_window_in_days = 30
secret_string = jsonencode({
engine = var.vector_database_secret_engine,
host = module.aurora_postgresql_v2.cluster_endpoint,
username = var.vector_database_bedrock_kb_username,
password = var.vector_database_bedrock_kb_password
dbClusterIdentifier = module.aurora_postgresql_v2.cluster_id,
port = var.vector_database_bedrock_port
})
tags = var.aws_resource_tags
depends_on = [module.aurora_postgresql_v2]
}
resource "null_resource" "db_setup" {
triggers = {
file = filesha1("scripts/bedrock.sql")
}
provisioner "local-exec" {
command = <<-EOF
while read line; do
echo "$line"
aws rds-data execute-statement --region "$REGION" --resource-arn "$DB_ARN" --database "$DB_NAME" --secret-arn "$SECRET_ARN" --sql "$line"
done < <(awk 'BEGIN{RS=";\n"}{gsub(/\n/,""); if(NF>0) {print $0";"}}' scripts/bedrock.sql)
EOF
environment = {
DB_ARN = module.aurora_postgresql_v2.cluster_arn
DB_NAME = module.aurora_postgresql_v2.cluster_database_name
SECRET_ARN = module.aurora_postgresql_v2.cluster_master_user_secret[0].secret_arn
REGION = data.aws_region.current.id
}
interpreter = ["bash", "-c"]
}
depends_on = [module.aurora_postgresql_v2]
}