-
Notifications
You must be signed in to change notification settings - Fork 1
/
iam.tf
109 lines (100 loc) · 3.27 KB
/
iam.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
# – IAM –
resource "aws_iam_role" "agent_role" {
count = var.create_agent ? 1 : 0
assume_role_policy = data.aws_iam_policy_document.agent_trust[0].json
name_prefix = var.name_prefix
}
resource "aws_iam_role_policy" "agent_policy" {
count = var.create_agent ? 1 : 0
policy = data.aws_iam_policy_document.agent_permissions[0].json
role = aws_iam_role.agent_role[0].id
}
resource "aws_iam_role_policy" "kb_policy" {
count = var.create_kb && var.create_agent ? 1 : 0
policy = data.aws_iam_policy_document.knowledge_base_permissions[0].json
role = aws_iam_role.agent_role[0].id
}
# Define the IAM role for Amazon Bedrock Knowledge Base
resource "aws_iam_role" "bedrock_knowledge_base_role" {
count = var.kb_role_arn != null || var.create_default_kb == false ? 0 : 1
name = "AmazonBedrockExecutionRoleForKnowledgeBase-${random_string.solution_prefix.result}"
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "bedrock.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
}
# Attach a policy to allow necessary permissions for the Bedrock Knowledge Base
resource "aws_iam_policy" "bedrock_knowledge_base_policy" {
count = var.kb_role_arn != null || var.create_default_kb == false ? 0 : 1
name = "AmazonBedrockKnowledgeBasePolicy-${random_string.solution_prefix.result}"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
],
"Resource" : var.kb_s3_data_source == null ? awscc_s3_bucket.s3_data_source[0].arn : var.kb_s3_data_source
},
{
"Effect" : "Allow",
"Action" : [
"s3:GetObject",
],
"Resource" : var.kb_s3_data_source == null ? "${awscc_s3_bucket.s3_data_source[0].arn}/*" : var.kb_s3_data_source
},
{
"Effect" : "Allow",
"Action" : [
"aoss:*"
],
"Resource" : awscc_opensearchserverless_collection.default_collection[0].arn
},
{
"Effect" : "Allow",
"Action" : [
"bedrock:InvokeModel",
],
"Resource" : var.kb_embedding_model_arn
},
{
"Effect" : "Allow",
"Action" : [
"bedrock:ListFoundationModels",
"bedrock:ListCustomModels"
],
"Resource" : "*"
},
]
})
}
# Attach the policy to the role
resource "aws_iam_role_policy_attachment" "bedrock_knowledge_base_policy_attachment" {
count = var.kb_role_arn != null || var.create_kb == false ? 0 : 1
role = aws_iam_role.bedrock_knowledge_base_role[0].name
policy_arn = aws_iam_policy.bedrock_knowledge_base_policy[0].arn
}
resource "aws_iam_role_policy" "bedrock_kb_oss" {
count = var.kb_role_arn != null || var.create_default_kb == false ? 0 : 1
name = "AmazonBedrockOSSPolicyForKnowledgeBase_${var.kb_name}"
role = aws_iam_role.bedrock_knowledge_base_role[count.index].name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = ["aoss:*"]
Effect = "Allow"
Resource = ["arn:aws:aoss:${local.region}:${local.account_id}:*/*"]
}
]
})
}