|
1 | 1 | import {Construct} from "constructs" |
2 | | -import {Duration} from "aws-cdk-lib" |
3 | | -import {PolicyStatement} from "aws-cdk-lib/aws-iam" |
4 | | -import {CfnCollection} from "aws-cdk-lib/aws-opensearchserverless" |
5 | | -import {AwsCustomResource, PhysicalResourceId, AwsCustomResourcePolicy} from "aws-cdk-lib/custom-resources" |
6 | | -import {LambdaFunction} from "../constructs/LambdaFunction" |
| 2 | +import {CfnCollection, CfnIndex} from "aws-cdk-lib/aws-opensearchserverless" |
7 | 3 |
|
8 | 4 | export interface VectorIndexProps { |
9 | 5 | readonly indexName: string |
10 | 6 | readonly collection: CfnCollection |
11 | | - readonly createIndexFunction: LambdaFunction |
12 | 7 | readonly endpoint: string |
13 | 8 | } |
14 | 9 |
|
15 | 10 | export class VectorIndex extends Construct { |
16 | | - public readonly vectorIndex: AwsCustomResource |
| 11 | + public readonly cfnIndex: CfnIndex |
17 | 12 |
|
18 | 13 | constructor(scope: Construct, id: string, props: VectorIndexProps) { |
19 | 14 | super(scope, id) |
20 | 15 |
|
21 | | - // Custom resource to manage OpenSearch vector index lifecycle via Lambda |
22 | | - this.vectorIndex = new AwsCustomResource(this, "VectorIndex", { |
23 | | - installLatestAwsSdk: true, |
24 | | - // Create index when stack is deployed |
25 | | - onCreate: { |
26 | | - service: "Lambda", |
27 | | - action: "invoke", |
28 | | - parameters: { |
29 | | - FunctionName: props.createIndexFunction.function.functionName, |
30 | | - InvocationType: "RequestResponse", |
31 | | - Payload: JSON.stringify({ |
32 | | - RequestType: "Create", |
33 | | - CollectionName: props.collection.name, |
34 | | - IndexName: props.indexName, |
35 | | - Endpoint: props.endpoint |
36 | | - }) |
| 16 | + const indexMapping: CfnIndex.MappingsProperty = { |
| 17 | + properties: { |
| 18 | + "bedrock-knowledge-base-default-vector": { |
| 19 | + type: "knn_vector", |
| 20 | + dimension: 1024, |
| 21 | + method: { |
| 22 | + name: "hnsw", |
| 23 | + engine: "faiss", |
| 24 | + parameters: {}, |
| 25 | + spaceType: "l2" |
| 26 | + } |
37 | 27 | }, |
38 | | - physicalResourceId: PhysicalResourceId.of(`VectorIndex-${props.indexName}`) |
39 | | - }, |
40 | | - // Delete index when stack is destroyed |
41 | | - onDelete: { |
42 | | - service: "Lambda", |
43 | | - action: "invoke", |
44 | | - parameters: { |
45 | | - FunctionName: props.createIndexFunction.function.functionName, |
46 | | - InvocationType: "RequestResponse", |
47 | | - Payload: JSON.stringify({ |
48 | | - RequestType: "Delete", |
49 | | - CollectionName: props.collection.name, |
50 | | - IndexName: props.indexName, |
51 | | - Endpoint: props.endpoint |
52 | | - }) |
| 28 | + "AMAZON_BEDROCK_METADATA": { |
| 29 | + type: "text", |
| 30 | + index: false |
| 31 | + }, |
| 32 | + "AMAZON_BEDROCK_TEXT_CHUNK": { |
| 33 | + type: "text", |
| 34 | + index: false |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const cfnIndex = new CfnIndex(this, "MyCfnIndex", { |
| 40 | + collectionEndpoint: props.endpoint, |
| 41 | + indexName: props.indexName, |
| 42 | + mappings: indexMapping, |
| 43 | + // the properties below are optional |
| 44 | + settings: { |
| 45 | + index: { |
| 46 | + knn: true, |
| 47 | + knnAlgoParamEfSearch: 512 |
53 | 48 | } |
54 | | - }, |
55 | | - // Grant permission to invoke the Lambda function |
56 | | - policy: AwsCustomResourcePolicy.fromStatements([ |
57 | | - new PolicyStatement({ |
58 | | - actions: ["lambda:InvokeFunction"], |
59 | | - resources: [props.createIndexFunction.function.functionArn] |
60 | | - }) |
61 | | - ]), |
62 | | - timeout: Duration.seconds(60) |
| 49 | + } |
63 | 50 | }) |
64 | 51 |
|
65 | 52 | // Ensure collection exists before creating index |
66 | | - this.vectorIndex.node.addDependency(props.collection) |
| 53 | + cfnIndex.node.addDependency(props.collection) |
| 54 | + this.cfnIndex = cfnIndex |
67 | 55 | } |
68 | 56 | } |
0 commit comments