Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deploy process Bedrock Agent (zip and container) using CloudFormation #437

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions examples/bedrock-agent-fastapi-zip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ The following tools should be installed and configured.
* [Python](https://www.python.org/)
* [Docker](https://www.docker.com/products/docker-desktop)

## Deploy to Lambda

Navigate to the sample's folder and use the SAM CLI to build a container image

```shell
sam build --use-container
```

This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.

To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen

```shell
sam deploy --guided
```

## Generate OpenAPI schema

Before you create your agent, you should set up action groups that you want to add to your agent. When you create an action group, you must define the APIs that the agent can invoke with an OpenAPI schema in JSON or YAML format. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-api-schema.html))
Expand All @@ -53,6 +37,7 @@ Please install the required dependency in a virtual environment first.
python3 -m venv .venv
source .venv/bin/activate
pip install -r app/requirements.txt
pip install boto3
cd app/
```

Expand All @@ -62,9 +47,37 @@ cd app/
python -c "import main;import json; print(json.dumps(main.app.openapi()))" > openapi.json
```

## Create an agent
## Update template.yaml

Update the Payload part of ActionGroups defined in template.yaml with the OpenAPI schema value.

```yaml
ApiSchema:
Payload: '<<Open API schema>>'
```

(in example root directory)

```shell
sed -i "s@\\\\n@\\\\\\\\\\\\\\\\n@g" app/openapi.json
sed -i "s@<<Open API schema>>@`cat app/openapi.json`@g" template.yaml
```

## Deploy to Lambda

Navigate to the sample's folder and use the SAM CLI to build a container image

```shell
sam build --use-container
```

This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.

see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html)
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen

```shell
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
```

## Test locally

Expand Down
55 changes: 55 additions & 0 deletions examples/bedrock-agent-fastapi-zip/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,62 @@ Resources:
SourceAccount: !Ref 'AWS::AccountId'
SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*

BedrockAgentResourceRole:
Type: AWS::IAM::Role
Properties:
RoleName: AmazonBedrockExecutionRoleForAgents_FastAPISample
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: bedrock.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
aws:SourceAccount: !Sub ${AWS::AccountId}
ArnLike:
aws:SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
Policies:
- PolicyName: AmazonBedrockExecutionRoleForAgents_FastAPISamplePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: bedrock:InvokeModel
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-v2:1
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !GetAtt BedrockAgentFastAPIFunction.Arn

BedrockAgent:
Type: AWS::Bedrock::Agent
Properties:
AgentName: BedrockAgentFastAPISample
Description: Query S3 information agent.
AgentResourceRoleArn: !GetAtt BedrockAgentResourceRole.Arn
Instruction: This agent allows you to query the S3 information in your AWS account.
FoundationModel: anthropic.claude-v2:1
ActionGroups:
- ActionGroupName: action-group
ActionGroupExecutor:
Lambda: !GetAtt BedrockAgentFastAPIFunction.Arn
ApiSchema:
Payload: '<<Open API schema>>'

BedrockAgentRelease:
Type: AWS::Bedrock::AgentAlias
Properties:
AgentAliasName: v1
AgentId: !Ref BedrockAgent

Outputs:
BedrockAgentFastAPIFunction:
Description: "BedrockAgentFastAPIFunction Lambda Function ARN"
Value: !GetAtt BedrockAgentFastAPIFunction.Arn
BedrockAgent:
Description: "BedrockAgent ID"
Value: !Ref BedrockAgent
BedrockAgentAlias:
Description: "BedrockAgentAlias ID"
Value: !Ref BedrockAgentRelease
48 changes: 29 additions & 19 deletions examples/bedrock-agent-fastapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ The following tools should be installed and configured.
* [Python](https://www.python.org/)
* [Docker](https://www.docker.com/products/docker-desktop)

## Deploy to Lambda

Navigate to the sample's folder and use the SAM CLI to build a container image

```shell
sam build
```

This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.

To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen

```shell
sam deploy --guided
```

## Generate OpenAPI schema

Before you create your agent, you should set up action groups that you want to add to your agent. When you create an action group, you must define the APIs that the agent can invoke with an OpenAPI schema in JSON or YAML format. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-api-schema.html))
Expand All @@ -69,11 +53,37 @@ cd app/
python -c "import main;import json; print(json.dumps(main.app.openapi()))" > openapi.json
```

## Create an agent
## Update template.yaml

Update the Payload part of ActionGroups defined in template.yaml with the OpenAPI schema value.

```yaml
ApiSchema:
Payload: '<<Open API schema>>'
```

see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html)
(in example root directory)

## Test locally
```shell
sed -i "s@\\\\n@\\\\\\\\\\\\\\\\n@g" app/openapi.json
sed -i "s@<<Open API schema>>@`cat app/openapi.json`@g" template.yaml
```

## Deploy to Lambda

Navigate to the sample's folder and use the SAM CLI to build a container image

```shell
sam build
```

This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.

To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen

```shell
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
```

Sample event exists in events directory. You can test locally with bellow command.

Expand Down
55 changes: 55 additions & 0 deletions examples/bedrock-agent-fastapi/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,62 @@ Resources:
SourceAccount: !Ref 'AWS::AccountId'
SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*

BedrockAgentResourceRole:
Type: AWS::IAM::Role
Properties:
RoleName: AmazonBedrockExecutionRoleForAgents_FastAPISample
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: bedrock.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
aws:SourceAccount: !Sub ${AWS::AccountId}
ArnLike:
aws:SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
Policies:
- PolicyName: AmazonBedrockExecutionRoleForAgents_FastAPISamplePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: bedrock:InvokeModel
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-v2:1
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !GetAtt BedrockAgentFastAPIFunction.Arn

BedrockAgent:
Type: AWS::Bedrock::Agent
Properties:
AgentName: BedrockAgentFastAPISample
Description: Query S3 information agent.
AgentResourceRoleArn: !GetAtt BedrockAgentResourceRole.Arn
Instruction: This agent allows you to query the S3 information in your AWS account.
FoundationModel: anthropic.claude-v2:1
ActionGroups:
- ActionGroupName: action-group
ActionGroupExecutor:
Lambda: !GetAtt BedrockAgentFastAPIFunction.Arn
ApiSchema:
Payload: '<<Open API schema>>'

BedrockAgentRelease:
Type: AWS::Bedrock::AgentAlias
Properties:
AgentAliasName: v1
AgentId: !Ref BedrockAgent

Outputs:
BedrockAgentFastAPIFunction:
Description: "BedrockAgentFastAPIFunction Lambda Function ARN"
Value: !GetAtt BedrockAgentFastAPIFunction.Arn
BedrockAgent:
Description: "BedrockAgent ID"
Value: !Ref BedrockAgent
BedrockAgentAlias:
Description: "BedrockAgentAlias ID"
Value: !Ref BedrockAgentRelease
Loading