Skip to content

Commit 36f4073

Browse files
authored
Update deploy process Bedrock Agent (zip and container) using CloudFormation (#437)
* update deploy process bedrock-agent-fastapi-zip * update deploy process bedrock-agent-fastapi
1 parent c556c1d commit 36f4073

File tree

4 files changed

+170
-37
lines changed

4 files changed

+170
-37
lines changed

examples/bedrock-agent-fastapi-zip/README.md

+31-18
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ The following tools should be installed and configured.
2525
* [Python](https://www.python.org/)
2626
* [Docker](https://www.docker.com/products/docker-desktop)
2727

28-
## Deploy to Lambda
29-
30-
Navigate to the sample's folder and use the SAM CLI to build a container image
31-
32-
```shell
33-
sam build --use-container
34-
```
35-
36-
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
37-
38-
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
39-
40-
```shell
41-
sam deploy --guided
42-
```
43-
4428
## Generate OpenAPI schema
4529

4630
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))
@@ -53,6 +37,7 @@ Please install the required dependency in a virtual environment first.
5337
python3 -m venv .venv
5438
source .venv/bin/activate
5539
pip install -r app/requirements.txt
40+
pip install boto3
5641
cd app/
5742
```
5843

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

65-
## Create an agent
50+
## Update template.yaml
51+
52+
Update the Payload part of ActionGroups defined in template.yaml with the OpenAPI schema value.
53+
54+
```yaml
55+
ApiSchema:
56+
Payload: '<<Open API schema>>'
57+
```
58+
59+
(in example root directory)
60+
61+
```shell
62+
sed -i "s@\\\\n@\\\\\\\\\\\\\\\\n@g" app/openapi.json
63+
sed -i "s@<<Open API schema>>@`cat app/openapi.json`@g" template.yaml
64+
```
65+
66+
## Deploy to Lambda
67+
68+
Navigate to the sample's folder and use the SAM CLI to build a container image
69+
70+
```shell
71+
sam build --use-container
72+
```
73+
74+
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
6675

67-
see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html)
76+
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
77+
78+
```shell
79+
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
80+
```
6881

6982
## Test locally
7083

examples/bedrock-agent-fastapi-zip/template.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,62 @@ Resources:
3636
SourceAccount: !Ref 'AWS::AccountId'
3737
SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
3838

39+
BedrockAgentResourceRole:
40+
Type: AWS::IAM::Role
41+
Properties:
42+
RoleName: AmazonBedrockExecutionRoleForAgents_FastAPISample
43+
AssumeRolePolicyDocument:
44+
Version: 2012-10-17
45+
Statement:
46+
- Effect: Allow
47+
Principal:
48+
Service: bedrock.amazonaws.com
49+
Action: sts:AssumeRole
50+
Condition:
51+
StringEquals:
52+
aws:SourceAccount: !Sub ${AWS::AccountId}
53+
ArnLike:
54+
aws:SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
55+
Policies:
56+
- PolicyName: AmazonBedrockExecutionRoleForAgents_FastAPISamplePolicy
57+
PolicyDocument:
58+
Version: 2012-10-17
59+
Statement:
60+
- Effect: Allow
61+
Action: bedrock:InvokeModel
62+
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-v2:1
63+
- Effect: Allow
64+
Action: lambda:InvokeFunction
65+
Resource: !GetAtt BedrockAgentFastAPIFunction.Arn
66+
67+
BedrockAgent:
68+
Type: AWS::Bedrock::Agent
69+
Properties:
70+
AgentName: BedrockAgentFastAPISample
71+
Description: Query S3 information agent.
72+
AgentResourceRoleArn: !GetAtt BedrockAgentResourceRole.Arn
73+
Instruction: This agent allows you to query the S3 information in your AWS account.
74+
FoundationModel: anthropic.claude-v2:1
75+
ActionGroups:
76+
- ActionGroupName: action-group
77+
ActionGroupExecutor:
78+
Lambda: !GetAtt BedrockAgentFastAPIFunction.Arn
79+
ApiSchema:
80+
Payload: '<<Open API schema>>'
81+
82+
BedrockAgentRelease:
83+
Type: AWS::Bedrock::AgentAlias
84+
Properties:
85+
AgentAliasName: v1
86+
AgentId: !Ref BedrockAgent
87+
3988
Outputs:
4089
BedrockAgentFastAPIFunction:
4190
Description: "BedrockAgentFastAPIFunction Lambda Function ARN"
4291
Value: !GetAtt BedrockAgentFastAPIFunction.Arn
92+
BedrockAgent:
93+
Description: "BedrockAgent ID"
94+
Value: !Ref BedrockAgent
95+
BedrockAgentAlias:
96+
Description: "BedrockAgentAlias ID"
97+
Value: !Ref BedrockAgentRelease

examples/bedrock-agent-fastapi/README.md

+29-19
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ The following tools should be installed and configured.
3232
* [Python](https://www.python.org/)
3333
* [Docker](https://www.docker.com/products/docker-desktop)
3434

35-
## Deploy to Lambda
36-
37-
Navigate to the sample's folder and use the SAM CLI to build a container image
38-
39-
```shell
40-
sam build
41-
```
42-
43-
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
44-
45-
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
46-
47-
```shell
48-
sam deploy --guided
49-
```
50-
5135
## Generate OpenAPI schema
5236

5337
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))
@@ -69,11 +53,37 @@ cd app/
6953
python -c "import main;import json; print(json.dumps(main.app.openapi()))" > openapi.json
7054
```
7155

72-
## Create an agent
56+
## Update template.yaml
57+
58+
Update the Payload part of ActionGroups defined in template.yaml with the OpenAPI schema value.
59+
60+
```yaml
61+
ApiSchema:
62+
Payload: '<<Open API schema>>'
63+
```
7364
74-
see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html)
65+
(in example root directory)
7566
76-
## Test locally
67+
```shell
68+
sed -i "s@\\\\n@\\\\\\\\\\\\\\\\n@g" app/openapi.json
69+
sed -i "s@<<Open API schema>>@`cat app/openapi.json`@g" template.yaml
70+
```
71+
72+
## Deploy to Lambda
73+
74+
Navigate to the sample's folder and use the SAM CLI to build a container image
75+
76+
```shell
77+
sam build
78+
```
79+
80+
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
81+
82+
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
83+
84+
```shell
85+
sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
86+
```
7787

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

examples/bedrock-agent-fastapi/template.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,62 @@ Resources:
3131
SourceAccount: !Ref 'AWS::AccountId'
3232
SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
3333

34+
BedrockAgentResourceRole:
35+
Type: AWS::IAM::Role
36+
Properties:
37+
RoleName: AmazonBedrockExecutionRoleForAgents_FastAPISample
38+
AssumeRolePolicyDocument:
39+
Version: 2012-10-17
40+
Statement:
41+
- Effect: Allow
42+
Principal:
43+
Service: bedrock.amazonaws.com
44+
Action: sts:AssumeRole
45+
Condition:
46+
StringEquals:
47+
aws:SourceAccount: !Sub ${AWS::AccountId}
48+
ArnLike:
49+
aws:SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*
50+
Policies:
51+
- PolicyName: AmazonBedrockExecutionRoleForAgents_FastAPISamplePolicy
52+
PolicyDocument:
53+
Version: 2012-10-17
54+
Statement:
55+
- Effect: Allow
56+
Action: bedrock:InvokeModel
57+
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-v2:1
58+
- Effect: Allow
59+
Action: lambda:InvokeFunction
60+
Resource: !GetAtt BedrockAgentFastAPIFunction.Arn
61+
62+
BedrockAgent:
63+
Type: AWS::Bedrock::Agent
64+
Properties:
65+
AgentName: BedrockAgentFastAPISample
66+
Description: Query S3 information agent.
67+
AgentResourceRoleArn: !GetAtt BedrockAgentResourceRole.Arn
68+
Instruction: This agent allows you to query the S3 information in your AWS account.
69+
FoundationModel: anthropic.claude-v2:1
70+
ActionGroups:
71+
- ActionGroupName: action-group
72+
ActionGroupExecutor:
73+
Lambda: !GetAtt BedrockAgentFastAPIFunction.Arn
74+
ApiSchema:
75+
Payload: '<<Open API schema>>'
76+
77+
BedrockAgentRelease:
78+
Type: AWS::Bedrock::AgentAlias
79+
Properties:
80+
AgentAliasName: v1
81+
AgentId: !Ref BedrockAgent
82+
3483
Outputs:
3584
BedrockAgentFastAPIFunction:
3685
Description: "BedrockAgentFastAPIFunction Lambda Function ARN"
3786
Value: !GetAtt BedrockAgentFastAPIFunction.Arn
87+
BedrockAgent:
88+
Description: "BedrockAgent ID"
89+
Value: !Ref BedrockAgent
90+
BedrockAgentAlias:
91+
Description: "BedrockAgentAlias ID"
92+
Value: !Ref BedrockAgentRelease

0 commit comments

Comments
 (0)