@@ -2,16 +2,8 @@ import {
22 App ,
33 Stack ,
44 StackProps ,
5- RemovalPolicy ,
65 CfnOutput
76} from "aws-cdk-lib"
8- import {
9- Bucket ,
10- BucketEncryption ,
11- BlockPublicAccess ,
12- ObjectOwnership
13- } from "aws-cdk-lib/aws-s3"
14- import { Key } from "aws-cdk-lib/aws-kms"
157import {
168 CfnGuardrail ,
179 CfnGuardrailVersion ,
@@ -28,6 +20,7 @@ import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager"
2820import { nagSuppressions } from "../nagSuppressions"
2921import { Apis } from "../resources/Apis"
3022import { Functions } from "../resources/Functions"
23+ import { Storage } from "../resources/Storage"
3124
3225const EMBEDDING_MODEL = "amazon.titan-embed-text-v2:0"
3326const COLLECTION_NAME = "eps-assist-vector-db"
@@ -90,51 +83,6 @@ export class EpsAssistMeStack extends Stack {
9083 tier : ssm . ParameterTier . STANDARD
9184 } )
9285
93- // Define the S3 bucket for access logs
94- const accessLogBucket = new Bucket ( this , "EpsAssistAccessLogsBucket" , {
95- blockPublicAccess : BlockPublicAccess . BLOCK_ALL ,
96- encryption : BucketEncryption . KMS ,
97- removalPolicy : RemovalPolicy . DESTROY ,
98- autoDeleteObjects : true ,
99- enforceSSL : true ,
100- versioned : false ,
101- objectOwnership : ObjectOwnership . BUCKET_OWNER_ENFORCED
102- } )
103-
104- // Create a customer-managed KMS key
105- const kbDocsKey = new Key ( this , "KbDocsKey" , {
106- enableKeyRotation : true ,
107- description : "KMS key for encrypting knowledge base documents"
108- } )
109-
110- // Use the KMS key in your S3 bucket
111- const kbDocsBucket = new Bucket ( this , "EpsAssistDocsBucket" , {
112- blockPublicAccess : BlockPublicAccess . BLOCK_ALL ,
113- encryption : BucketEncryption . KMS ,
114- encryptionKey : kbDocsKey ,
115- removalPolicy : RemovalPolicy . DESTROY ,
116- autoDeleteObjects : true ,
117- enforceSSL : true ,
118- versioned : true ,
119- objectOwnership : ObjectOwnership . BUCKET_OWNER_ENFORCED ,
120- serverAccessLogsBucket : accessLogBucket ,
121- serverAccessLogsPrefix : "s3-access-logs/"
122- } )
123-
124- // Create an IAM policy for S3 access
125- const s3AccessListPolicy = new PolicyStatement ( {
126- actions : [ "s3:ListBucket" ] ,
127- resources : [ kbDocsBucket . bucketArn ]
128- } )
129- s3AccessListPolicy . addCondition ( "StringEquals" , { "aws:ResourceAccount" : account } )
130-
131- // Create an IAM policy for S3 access
132- const s3AccessGetPolicy = new PolicyStatement ( {
133- actions : [ "s3:GetObject" , "s3:Delete*" ] ,
134- resources : [ `${ kbDocsBucket . bucketArn } /*` ]
135- } )
136- s3AccessGetPolicy . addCondition ( "StringEquals" , { "aws:ResourceAccount" : account } )
137-
13886 // Create an IAM policy to invoke Bedrock models and access titan v1 embedding model
13987 const bedrockExecutionRolePolicy = new PolicyStatement ( )
14088 bedrockExecutionRolePolicy . addActions ( "bedrock:InvokeModel" )
@@ -164,17 +112,29 @@ export class EpsAssistMeStack extends Stack {
164112 } )
165113 bedrockExecutionRole . addToPolicy ( bedrockExecutionRolePolicy )
166114 bedrockExecutionRole . addToPolicy ( bedrockOSSPolicyForKnowledgeBase )
167- bedrockExecutionRole . addToPolicy ( s3AccessListPolicy )
168- bedrockExecutionRole . addToPolicy ( s3AccessGetPolicy )
169115 bedrockExecutionRole . addToPolicy ( bedrockKBDeleteRolePolicy )
170116
171- // Grant Bedrock permission to decrypt
172- kbDocsKey . addToResourcePolicy ( new iam . PolicyStatement ( {
173- effect : iam . Effect . ALLOW ,
174- principals : [ new iam . ArnPrincipal ( bedrockExecutionRole . roleArn ) ] ,
175- actions : [ "kms:Decrypt" , "kms:DescribeKey" ] ,
176- resources : [ "*" ]
177- } ) )
117+ // Create Storage construct
118+ const storage = new Storage ( this , "Storage" , {
119+ bedrockExecutionRole
120+ } )
121+
122+ // Create an IAM policy for S3 access
123+ const s3AccessListPolicy = new PolicyStatement ( {
124+ actions : [ "s3:ListBucket" ] ,
125+ resources : [ storage . kbDocsBucket . bucketArn ]
126+ } )
127+ s3AccessListPolicy . addCondition ( "StringEquals" , { "aws:ResourceAccount" : account } )
128+
129+ // Create an IAM policy for S3 access
130+ const s3AccessGetPolicy = new PolicyStatement ( {
131+ actions : [ "s3:GetObject" , "s3:Delete*" ] ,
132+ resources : [ `${ storage . kbDocsBucket . bucketArn } /*` ]
133+ } )
134+ s3AccessGetPolicy . addCondition ( "StringEquals" , { "aws:ResourceAccount" : account } )
135+
136+ bedrockExecutionRole . addToPolicy ( s3AccessListPolicy )
137+ bedrockExecutionRole . addToPolicy ( s3AccessGetPolicy )
178138
179139 // Create bedrock Guardrails for the slack bot
180140 const guardrail = new CfnGuardrail ( this , "EpsGuardrail" , {
@@ -400,7 +360,7 @@ export class EpsAssistMeStack extends Stack {
400360 dataSourceConfiguration : {
401361 type : "S3" ,
402362 s3Configuration : {
403- bucketArn : kbDocsBucket . bucketArn
363+ bucketArn : storage . kbDocsBucket . bucketArn
404364 }
405365 }
406366 } )
0 commit comments