16
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

本記事は セゾンテクノロジー Advent Calendar 2025 1 日目の記事です。

Amazon S3 Vectors と Amazon Bedrock ナレッジベースの統合は 2025/12/1 時点で Preview として提供されています。将来的に動作や仕様が変更される可能性があります。

はじめに

Amazon Bedrock ナレッジベースのベクトルストアに S3 Vectors を使用すると、1 秒未満のクエリ待機時間を許容できる場合に大幅にコストを抑えて RAG アプリケーションを構築できます。

AWS Whas's New 等では特に案内されていませんが、2025/10/31 に CloudFormation が S3 Vectors をサポートしました。また 11 月の下旬頃に AWS::Bedrock::KnowledgeBase リソースもアップデートされ、S3 Vectors を使用した構成を CloudFormation テンプレートで一撃デプロイできるようになりました。

本記事ではテンプレートの例を紹介するとともに、実装時のポイントなどを解説します。テンプレート全体は記事の最後に載せています。

ベクトルバケットの作成

標準の暗号化 (AES256) でよければ 2 行で済みます。簡単ですね!ただし、ベクトルバケットの作成後に暗号化方式の変更 (AES256 ←→ awskms) はできない点にはご注意ください。

  KBVectorBucket:
    Type: AWS::S3Vectors::VectorBucket

戻り値

リソース 宣言例 取得できる値
AWS::S3Vectors::VectorBucket !Ref ResourceName ベクトルバケット名
AWS::S3Vectors::VectorBucket !GetAtt ResourceName.VectorBucketArn ベクトルバケットの ARN

ベクトルインデックスの作成

ベクトルインデックスも作成しておく必要があります。Knowledge Bases のコンソールでは設定値等を意識せずに簡単に作成できるため、設定値として一番わかりづらいのはこの部分かもしれません。

  KBVectorIndex:
    Type: AWS::S3Vectors::Index
    Properties:
      VectorBucketArn: !GetAtt KBVectorBucket.VectorBucketArn
      IndexName: bedrock-knowledge-base-default-index
      DataType: float32
      Dimension: 1024
      DistanceMetric: euclidean
      MetadataConfiguration: 
        NonFilterableMetadataKeys:
          - AMAZON_BEDROCK_TEXT
          - AMAZON_BEDROCK_METADATA

インデックス名は固定で bedrock-knowledge-base-default-index とします。

また NonFilterableMetadataKeys にフィルタリング不可のメタデータキーとして、AMAZON_BEDROCK_TEXT および AMAZON_BEDROCK_METADATA を指定しています。

ベクターデータをインデックスへ追加する際に、各ベクターにメタデータを添付できます。デフォルトでは、ベクターに添付されたすべてのメタデータはフィルタリング可能であり、類似検索クエリのフィルターとして使用できます。

しかしフィルタリング可能なメタデータはベクターごとに最大 2 KB までという制限があり、テキストチャンクが2KB のメタデータ領域を超えることが予想される場合はこれらのキーを追加することが推奨されます。

Note
If you expect your text chunks to exceed the 2 KB metadata space, we recommend that you add the text field AMAZON_BEDROCK_TEXT and AMAZON_BEDROCK_METADATA as non-filterable metadata keys. Your knowledge base will use these fields to store the text chunks and system metadata.

ただし NonFilterableMetadataKeys を指定しない場合は通常の PDF ファイルレベルでも 2 KB の制限値エラーでデータソースの同期がエラーになることを確認しています。そのため、ほぼ必須と考えてよいかと思います。

戻り値

リソース 宣言例 取得できる値
AWS::S3Vectors::Index !Ref ResourceName インデックスの ARN
AWS::S3Vectors::Index !GetAtt ResourceName.CreationTime インデックスの作成時間
AWS::S3Vectors::Index !GetAtt ResourceName.CreationTime インデックスの ARN

ベクトルバケットポリシー

バケットポリシーの作成も CloudFormation でサポートされています。必須ではないため今回テンプレート例では割愛しています。

Knowledge Bases 用のロールにアタッチするポリシーの作成

作成したインデックスに対してベクターデータを操作するための権限を付与します。

  BedrockS3VectorsPolicyForKB:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: !Sub ${AWS::StackName}-bedrock-s3vectors-policy
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: S3VectorsStatement
            Effect: Allow
            Action:
              - 's3vectors:GetIndex'
              - 's3vectors:QueryVectors'
              - 's3vectors:PutVectors'
              - 's3vectors:GetVectors'
              - 's3vectors:DeleteVectors'
            Resource: !Ref KBVectorIndex

Knowledge Bases の Storage Configuration

Type: S3_VECTORS および S3VectorsConfiguration が追加されています。

  BedrockKnowledgeBase:
    Type: AWS::Bedrock::KnowledgeBase
    DependsOn: BedrockS3VectorsPolicyForKB
    Properties:
      Name: !Sub ${AWS::StackName}-knowledge-base
      KnowledgeBaseConfiguration:
        Type: VECTOR
        VectorKnowledgeBaseConfiguration:
          EmbeddingModelArn: !Sub arn:${AWS::Partition}:bedrock:${AWS::Region}::foundation-model/${EmbeddingModelId}
      RoleArn: !GetAtt BedrockKnowledgeBaseRole.Arn
      StorageConfiguration:
        Type: S3_VECTORS
        S3VectorsConfiguration:
          IndexArn: !Ref KBVectorIndex
          VectorBucketArn: !GetAtt KBVectorBucket.VectorBucketArn

S3VectorsConfiguration の構文としては以下の 3 つのプロパティを指定できますが、すべてを同時に指定すると作成エラーになるため、IndexArnVectorBucketArn の組み合わせとしています。設定値としては極めてシンプルですね。

  IndexArn: String
  IndexName: String
  VectorBucketArn: String

また本記事の検証時点では DependsOn にベクトルデータを操作するための IAM ポリシー (本記事では BedrockS3VectorsPolicyForKB) を追加する必要がありました。

テンプレート例 (全体)

AWSTemplateFormatVersion: 2010-09-09
Description: 'Amazon Bedrock Knowledge Bases with S3 Vectors'

Parameters:
  EmbeddingModelId:
    Type: String
    Default: amazon.titan-embed-text-v2:0
    Description: 'The Id of the Bedrock model that is used to generate embeddings.'

Resources:
  KBVectorBucket:
    Type: AWS::S3Vectors::VectorBucket

  KBVectorIndex:
    Type: AWS::S3Vectors::Index
    Properties:
      VectorBucketArn: !GetAtt KBVectorBucket.VectorBucketArn
      IndexName: bedrock-knowledge-base-default-index
      DataType: float32
      Dimension: 1024
      DistanceMetric: euclidean
      MetadataConfiguration: 
        NonFilterableMetadataKeys:
          - AMAZON_BEDROCK_TEXT
          - AMAZON_BEDROCK_METADATA

  BedrockFMPolicyForKB:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: !Sub ${AWS::StackName}-bedrock-fm-policy
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: BedrockInvokeModelStatement
            Effect: Allow
            Action:
              - 'bedrock:InvokeModel'
            Resource: !Sub arn:${AWS::Partition}:bedrock:${AWS::Region}::foundation-model/${EmbeddingModelId}
      Roles:
        - !Ref BedrockKnowledgeBaseRole

  BedrockS3VectorsPolicyForKB:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: !Sub ${AWS::StackName}-bedrock-s3vectors-policy
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: S3VectorsStatement
            Effect: Allow
            Action:
              - 's3vectors:GetIndex'
              - 's3vectors:QueryVectors'
              - 's3vectors:PutVectors'
              - 's3vectors:GetVectors'
              - 's3vectors:DeleteVectors'
            Resource: !Ref KBVectorIndex
            Condition:
              StringEquals:
                aws:ResourceAccount: !Ref 'AWS::AccountId'
      Roles:
        - !Ref BedrockKnowledgeBaseRole

  BedrockS3PolicyForKB:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: !Sub ${AWS::StackName}-bedrock-s3-policy
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: S3ListBucketStatement
            Effect: Allow
            Action:
              - 's3:ListBucket'
            Resource: !GetAtt  DataSourceBucket.Arn
            Condition:
              StringEquals:
                aws:ResourceAccount: !Ref 'AWS::AccountId'
          - Sid: S3GetObjectStatement
            Effect: Allow
            Action:
              - 's3:GetObject'
            Resource:
              - !Sub 'arn:${AWS::Partition}:s3:::${DataSourceBucket}/*'
            Condition:
              StringEquals:
                aws:ResourceAccount: !Ref 'AWS::AccountId'
      Roles:
        - !Ref BedrockKnowledgeBaseRole

  BedrockKnowledgeBaseRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub ${AWS::StackName}-bedrock-kb-role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: [bedrock.amazonaws.com]
            Action: ['sts:AssumeRole']

  BedrockKnowledgeBase:
    Type: AWS::Bedrock::KnowledgeBase
    DependsOn: BedrockS3VectorsPolicyForKB
    Properties:
      Name: !Sub ${AWS::StackName}-knowledge-base
      KnowledgeBaseConfiguration:
        Type: VECTOR
        VectorKnowledgeBaseConfiguration:
          EmbeddingModelArn: !Sub arn:${AWS::Partition}:bedrock:${AWS::Region}::foundation-model/${EmbeddingModelId}
      RoleArn: !GetAtt BedrockKnowledgeBaseRole.Arn
      StorageConfiguration:
        Type: S3_VECTORS
        S3VectorsConfiguration:
          IndexArn: !Ref KBVectorIndex
          VectorBucketArn: !GetAtt KBVectorBucket.VectorBucketArn

  BedrockKnowledgeBaseDS:
    Type: AWS::Bedrock::DataSource
    Properties:
      DataDeletionPolicy: RETAIN
      KnowledgeBaseId: !Ref BedrockKnowledgeBase
      Name: !Sub ${AWS::StackName}-data-source
      DataSourceConfiguration:
        Type: S3
        S3Configuration:
          BucketArn: !GetAtt DataSourceBucket.Arn
      VectorIngestionConfiguration:
        ChunkingConfiguration:
          ChunkingStrategy: HIERARCHICAL
          HierarchicalChunkingConfiguration:
            LevelConfigurations:
              - MaxTokens: 1500
              - MaxTokens: 300
            OverlapTokens: 60

  DataSourceBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub '${AWS::StackName}-bedrock-kb-ds-bucket-${AWS::AccountId}'
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      OwnershipControls:
        Rules:
          - ObjectOwnership: BucketOwnerEnforced

Outputs:
  BedrockKnowledgeBaseId:
    Value: !Ref BedrockKnowledgeBase
    Description: 'Bedrock Knowledge Base ID'
  BedrockDataSourceId:
    Value: !Ref BedrockKnowledgeBaseDS
    Description: 'Bedrock Knowledge Base Data Source ID (Format: KnowledgeBaseId|DataSourceId)'
  DSBucketName:
    Value: !Ref DataSourceBucket
    Description: 'Name for Knowledge Base Data Source S3 Bucket'

参考

今回作成したテンプレートのベース部分は以下の記事を参考にさせていただきました。

以上です。
参考になれば幸いです。

16
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?