New serverless pattern - Strands Agent on Lambda with AgentCore Smithy Bedrock Target#3221
Conversation
| Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - bedrock-agentcore:* |
There was a problem hiding this comment.
Instead of wild card (*) access, can you make it restricted to required actions only.
| Resource: | ||
| - !Sub 'arn:aws:bedrock:${AWS::Region}::foundation-model/*' | ||
| - !Sub 'arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/*' | ||
| - '*' |
There was a problem hiding this comment.
Wildcard (*) opens up everything. Can you make it restricted to required resources only.
| - bedrock:InvokeModelWithResponseStream | ||
| - bedrock:Converse | ||
| - bedrock:ConverseStream | ||
| Resource: '*' |
There was a problem hiding this comment.
Wildcard (*) opens up everything. Can you make it restricted to required resources only.
| - Effect: Allow | ||
| Principal: | ||
| Service: bedrock-agentcore.amazonaws.com | ||
| Action: sts:AssumeRole |
There was a problem hiding this comment.
Can you try adding the condition? Please test after adding. Adding these conditions to prevent the cross-service confused deputy problem
AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: bedrock-agentcore.amazonaws.com Action: sts:AssumeRole Condition: StringEquals: aws:SourceAccount: !Ref AWS::AccountId ArnLike: aws:SourceArn: !Sub 'arn:aws:bedrock-agentcore:${AWS::Region}:${AWS::AccountId}:*'
| - logs:CreateLogGroup | ||
| - logs:CreateLogStream | ||
| - logs:PutLogEvents | ||
| Resource: '*' |
There was a problem hiding this comment.
Can you make it more restrictive? e.g.
Resource: - !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}-AgentFunction:*'
Test and modify if needed.
| if isinstance(body, dict): | ||
| prompt = body.get("prompt", "") | ||
| elif isinstance(body, str): | ||
| import json |
There was a problem hiding this comment.
move the import statement at the top of the file to maintain standard.
| Transform: AWS::Serverless-2016-10-31 | ||
| Description: > | ||
| AgentCore Smithy Bedrock Runtime - Serverless AI agent exposing Bedrock Runtime | ||
| operations through AWS Bedrock AgentCore Gateway using a Smithy model target. |
There was a problem hiding this comment.
AWS Bedrock AgentCore --> Amazon Bedrock AgentCore
Bedrock --> Amazon Bedrock
|
|
||
|
|
||
| def create_mcp_client(gateway_url: str, token: str) -> MCPClient: | ||
| """Create an MCP client configured to connect to AgentCore Gateway. |
There was a problem hiding this comment.
AgentCore Gateway --> Amazon Bedrock AgentCore Gateway
| Configures Claude 3 Sonnet as the LLM provider via AWS Bedrock. | ||
|
|
||
| Args: | ||
| model_id: Bedrock model identifier. Defaults to Claude 3 Sonnet. |
There was a problem hiding this comment.
Update the model id accordingly.
| One command deploys everything: | ||
|
|
||
| ```bash | ||
| bash scripts/deploy.sh |
There was a problem hiding this comment.
Deploy failed with the following issue:
`strands-agentcore-smithy % bash scripts/deploy.sh
Deploying stack: agentcore-smithy-bedrock in region: us-east-1
Step 1: Validating SAM template...
SAM CLI update available (1.163.0); (1.162.1 installed)
To download: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Template validation successful.
Step 2: Uploading Bedrock Runtime Smithy model to S3...
upload failed: ../../../../../../../tmp/bedrock-runtime-2023-09-30.json to s3://agentcore-smithy-bedrock-smithy-models/bedrock-runtime-2023-09-30.json An error occurred (AccessDenied) when calling the PutObject operation: Access Denied`
Issue #, if available:
Description of changes:
Strands SDK agent on Lambda calls Bedrock Runtime via AgentCore Gateway using a Smithy
model target, with Cognito JWT auth and IAM role signing.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.