AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-07-13 · Documentation low

File: code-library/latest/ug/python_3_bedrock-agent_code_examples.md

Summary

Added ListKnowledgeBases code example for enumerating knowledge bases

Security assessment

Basic resource enumeration documentation with no security implications or features addressed.

Diff

diff --git a/code-library/latest/ug/python_3_bedrock-agent_code_examples.md b/code-library/latest/ug/python_3_bedrock-agent_code_examples.md
index 97d6213c5..e638189af 100644
--- a//code-library/latest/ug/python_3_bedrock-agent_code_examples.md
+++ b//code-library/latest/ug/python_3_bedrock-agent_code_examples.md
@@ -333,0 +334,76 @@ Create a version of an Amazon Bedrock flow.
+The following code example shows how to use `CreateKnowledgeBase`.
+
+**SDK for Python (Boto3)**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-agent#code-examples). 
+
+Create an Amazon Bedrock knowledge base.
+    
+    
+    def create_knowledge_base(bedrock_agent_client, name, role_arn, description=None):
+        """
+        Creates a new knowledge base.
+    
+        Args:
+            bedrock_agent_client: The Boto3 Bedrock Agent client.
+            name (str): The name of the knowledge base.
+            role_arn (str): The ARN of the IAM role that the knowledge base assumes to access resources.
+            description (str, optional): A description of the knowledge base.
+    
+        Returns:
+            dict: The details of the created knowledge base.
+        """
+        try:
+            kwargs = {
+                "name": name,
+                "roleArn": role_arn,
+                "knowledgeBaseConfiguration": {
+                    "type": "VECTOR",
+                    "vectorKnowledgeBaseConfiguration": {
+                        "embeddingModelArn": "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v1"
+                    }
+                },
+                "storageConfiguration": {
+                    "type": "OPENSEARCH_SERVERLESS",
+                    # Note: You will need to create an OpenSearch Serverless collection first and replace this ARN
+                    # with your actual collection ARN from the OpenSearch console. If you use the console instead,
+                    # you can use the quick-create flow to have Knowledge Bases create the collection for you.
+                    "opensearchServerlessConfiguration": {
+                        "collectionArn": "arn:aws:aoss:us-east-1::123456789012:collection/abcdefgh12345678defgh",
+                            "fieldMapping": {
+                            "metadataField": "metadata",
+                            "textField": "text",
+                            "vectorField": "vector"
+                            },
+                        "vectorIndexName": "test-uuid"
+                        },
+                    },
+                "clientToken": "test-client-token-" + str(uuid.uuid4())
+            }
+            
+            if description:
+                kwargs["description"] = description
+                
+            response = bedrock_agent_client.create_knowledge_base(**kwargs)
+            
+            logger.info("Created knowledge base with ID: %s", response["knowledgeBase"]["knowledgeBaseId"])
+            return response["knowledgeBase"]
+        
+        except ClientError as err:
+            logger.error(
+                "Couldn't create knowledge base. Here's why: %s: %s",
+                err.response["Error"]["Code"],
+                err.response["Error"]["Message"],
+            )
+            raise
+    
+    
+
+  * For API details, see [CreateKnowledgeBase](https://docs.aws.amazon.com/goto/boto3/bedrock-agent-2023-12-12/CreateKnowledgeBase) in _AWS SDK for Python (Boto3) API Reference_. 
+
+
+
+
@@ -706,0 +783,46 @@ Delete a version of an Amazon Bedrock flow.
+The following code example shows how to use `DeleteKnowledgeBase`.
+
+**SDK for Python (Boto3)**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-agent#code-examples). 
+
+Delete an Amazon Bedrock knowledge base.
+    
+    
+    def delete_knowledge_base(bedrock_agent_client, knowledge_base_id):
+        """
+        Deletes a knowledge base.
+    
+        Args:
+            bedrock_agent_client: The Boto3 Bedrock Agent client.
+            knowledge_base_id (str): The ID of the knowledge base to delete.
+    
+        Returns:
+            bool: True if the deletion was successful.
+        """
+        try:
+            bedrock_agent_client.delete_knowledge_base(
+                knowledgeBaseId=knowledge_base_id
+            )
+            
+            logger.info("Deleted knowledge base: %s", knowledge_base_id)
+            return True
+        except ClientError as err:
+            logger.error(
+                "Couldn't delete knowledge base %s. Here's why: %s: %s",
+                knowledge_base_id,
+                err.response["Error"]["Code"],
+                err.response["Error"]["Message"],
+            )
+            raise
+    
+    
+
+  * For API details, see [DeleteKnowledgeBase](https://docs.aws.amazon.com/goto/boto3/bedrock-agent-2023-12-12/DeleteKnowledgeBase) in _AWS SDK for Python (Boto3) API Reference_. 
+
+
+
+
@@ -904,0 +1027,46 @@ Get a version of an Amazon Bedrock flow.
+The following code example shows how to use `GetKnowledgeBase`.
+
+**SDK for Python (Boto3)**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-agent#code-examples). 
+
+Get an Amazon Bedrock knowledge base.
+    
+    
+    def get_knowledge_base(bedrock_agent_client, knowledge_base_id):
+        """
+        Gets details about a specific knowledge base.
+    
+        Args:
+            bedrock_agent_client: The Boto3 Bedrock Agent client.
+            knowledge_base_id (str): The ID of the knowledge base.
+    
+        Returns:
+            dict: The details of the knowledge base.
+        """
+        try:
+            response = bedrock_agent_client.get_knowledge_base(
+                knowledgeBaseId=knowledge_base_id
+            )
+            
+            logger.info("Retrieved knowledge base: %s", knowledge_base_id)
+            return response["knowledgeBase"]
+        except ClientError as err:
+            logger.error(
+                "Couldn't get knowledge base %s. Here's why: %s: %s",
+                knowledge_base_id,
+                err.response["Error"]["Code"],
+                err.response["Error"]["Message"],
+            )
+            raise
+    
+    
+
+  * For API details, see [GetKnowledgeBase](https://docs.aws.amazon.com/goto/boto3/bedrock-agent-2023-12-12/GetKnowledgeBase) in _AWS SDK for Python (Boto3) API Reference_. 
+
+
+
+
@@ -1286,0 +1455,56 @@ List Amazon Bedrock flows.
+The following code example shows how to use `ListKnowledgeBases`.
+
+**SDK for Python (Boto3)**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/bedrock-agent#code-examples). 
+
+List Amazon Bedrock knowledge Bases.
+    
+    
+    def list_knowledge_bases(bedrock_agent_client, max_results=None):
+        """
+        Lists the knowledge bases in your AWS account.
+    
+        Args:
+            bedrock_agent_client: The Boto3 Bedrock Agent client.
+            max_results (int, optional): The maximum number of knowledge bases to return.
+    
+        Returns:
+            list: A list of knowledge base details.
+        """
+        try: