Skip to content

Irrelevant error when deleting bedrock knowledge base using boto3

0

I am trying to delete a Bedrock knowledge base using Python SDK (Boto3)

client.delete_knowledge_base(knowledgeBaseId=kb_id)

When the provided kb_id does not follow the required format (I assume it should be ^[A-Z0-9]{10}$), for example, when I provide Knowledge Base name instead of Knowledge Base ID or when passing E94N4QO8 (8 characters), then it raises an irrelevant error:

botocore.errorfactory.AccessDeniedException:
An error occurred (AccessDeniedException) when calling the DeleteKnowledgeBase operation:
User: arn:aws:iam::?????:user/????? is not authorized to perform: bedrock:DeleteKnowledgeBase

The error is misleading because the user has permission for bedrock:DeleteKnowledgeBase.

My expected behaviour is to either validate the format of knowledgeBaseId and raise a validation error, or at least a ResourceNotFoundException

2 Answers
1

Hi There

I tested this myself and see the same error using the CLI.

~ $ aws bedrock-agent delete-knowledge-base --knowledge-base-id ABCDEFGH (non-existant KB ID)

An error occurred (AccessDeniedException) when calling the DeleteKnowledgeBase operation: User: arn:aws:sts::123456789012:assumed-role/*****/********* is not authorized to perform: bedrock:DeleteKnowledgeBase

Thanks for raising this, I'll go ahead and raise the issue to the Bedrock service team for further investigation

AWS
EXPERT

answered a year ago

-1

When you're trying to delete a Bedrock knowledge base using the AWS SDK for Python (Boto3) and providing an incorrectly formatted knowledge base ID, you're encountering a misleading error message about permissions rather than a validation error.

The delete_knowledge_base function in Boto3 expects a valid knowledge base ID in the correct format. When you provide an invalid format (like a knowledge base name instead of ID, or an ID with incorrect length), the API should ideally return a validation error, but instead returns an AccessDeniedException.

This appears to be an issue with how the Bedrock service handles invalid IDs. The proper way to use this function is:

def delete_knowledge_base(bedrock_agent_client, knowledge_base_id):
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

To work around this issue, you should:

  1. Ensure you're using the correct knowledge base ID format (typically a 10-character alphanumeric string)
  2. Validate the ID format in your code before making the API call
  3. Handle the exception appropriately, recognizing that an AccessDeniedException might actually indicate an invalid ID format rather than a permissions issue

This behavior is unexpected and could be considered a bug in how the Bedrock service handles invalid IDs.
Sources
Use DeleteKnowledgeBase with an AWS SDK - Amazon Bedrock
Use DeleteKnowledgeBase with an AWS SDK - AWS SDK Code Examples
Amazon Bedrock Agents examples using SDK for Python (Boto3) - AWS SDK Code Examples

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.