[BUG] AWS::Bedrock::KnowledgeBase resource creation via AgentKnowledgeBase construct fails due to the lack of permission for KnowledgeBase role depending on the timing. #22
These statements generate AWS::IAM::Policy resource in CloudFormation template. However, AWS::Bedrock::KnowledgeBase resource does NOT depend on the AWS::IAM::Policy resource. As a result, CloudFormation will try to create AWS::Bedrock::KnowledgeBase and AWS::IAM::Policy for KB role in parallel. If the creation for AWS::Bedrock::KnowledgeBase resource is initiated before AWS::IAM::Policy resource for KB role depending on the timing, then CreateKnowldgeBase API will fail due to the lack of permission for KB role.
* Note: If the creation for the AWS::IAM::Policy resource completes luckily before initiating creation for AWS::Bedrock::KnowledgeBase, the issue will not occur.
When the issue occurred, I confirmed InvokeModel API call by KB role failed with AccessDenied exception and then CreateKnowldgeBase API call by CloudFormation failed with ValidationException in CloudTrail logs.
"eventTime": "2024-08-17T01:14:36Z",
"eventSource": "bedrock.amazonaws.com",
"eventName": "InvokeModel",
"awsRegion": "us-east-1",
"sourceIPAddress": "bedrock.amazonaws.com",
"userAgent": "bedrock.amazonaws.com",
"errorCode": "AccessDenied",
"errorMessage": "User: arn:aws:sts::xxxxxxxxxxxx:assumed-role/AgentWithKBandGuardrailsS-BedrockDocsBedrockKBServi-OeBY4e6ms0pm/BedrockKnowledgeBaseCPSession-IGJDOHN98G is not authorized to perform: bedrock:InvokeModel on resource: arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-embed-text-v1 because no identity-based policy allows the bedrock:InvokeModel action",
Description of the issue
AWS::Bedrock::KnowledgeBase resource creation via AgentKnowledgeBase construct fails due to the lack of permission for KnowledgeBase role.
Reproductions steps
I used 03-agent-with-kb-and-guardrails in amazon-bedrock-samples repository as a sample.
Investigation
From AgentKnowledgeBase construct, KnowledgeBase role (KB role) is created. https://github.com/awslabs/agents-for-amazon-bedrock-blueprints/blob/fdd45ca8d5198717ee9b84b7ececbecf69091d9e/lib/constructs/AgentKnowledgeBase.ts#L150-L169
For this role, some necessary permissions are granted by addToPolicy method.
https://github.com/awslabs/agents-for-amazon-bedrock-blueprints/blob/fdd45ca8d5198717ee9b84b7ececbecf69091d9e/lib/constructs/AgentKnowledgeBase.ts#L166 https://github.com/awslabs/agents-for-amazon-bedrock-blueprints/blob/fdd45ca8d5198717ee9b84b7ececbecf69091d9e/lib/constructs/AgentKnowledgeBase.ts#L193 https://github.com/awslabs/agents-for-amazon-bedrock-blueprints/blob/fdd45ca8d5198717ee9b84b7ececbecf69091d9e/lib/constructs/AgentKnowledgeBase.ts#L379
These statements generate
AWS::IAM::Policy
resource in CloudFormation template. However,AWS::Bedrock::KnowledgeBase
resource does NOT depend on theAWS::IAM::Policy
resource. As a result, CloudFormation will try to createAWS::Bedrock::KnowledgeBase
andAWS::IAM::Policy
for KB role in parallel. If the creation forAWS::Bedrock::KnowledgeBase
resource is initiated beforeAWS::IAM::Policy
resource for KB role depending on the timing, then CreateKnowldgeBase API will fail due to the lack of permission for KB role. * Note: If the creation for theAWS::IAM::Policy
resource completes luckily before initiating creation forAWS::Bedrock::KnowledgeBase
, the issue will not occur.When the issue occurred, I confirmed InvokeModel API call by KB role failed with AccessDenied exception and then CreateKnowldgeBase API call by CloudFormation failed with ValidationException in CloudTrail logs.