This project contains a template for using AWS Powertools and CDK to deploy lambda-powered Bedrock Agents. Each of these things, CDK, Powertools, Lambda, and Bedrock, are awesome and powerful. This template shows you how to use them all together.
Comments, suggestions, pull requests, are very welcome.
Author: Stephen J. Barr (@stevejb on X).
The core of the stack is seen in the cdk_lambda_bedrock/cdk_lambda_bedrock_stack.py
folder.
This stack contains:
- A single Agent
- A reference to an existing Lambda Layer for AWS Power Tools
- Two Action Groups
- For each Action Group, a
PythonFunction
- The
PythonFunction
's share a common Lambda Layer. This is provided here.
graph TD;
A[Stack] --> B[Agent]
B --> C[AgentActionGroup - TimeFunctions]
B --> D[AgentActionGroup - TodoListManagementFunctions]
C --> E[LambdaFunction - TimeFunctions<br/><code>lambda/app.py</code>]
D --> F[LambdaFunction - TodoListManagementFunctions<br/><code>lambda_two/app.py</code>]
E --> G[AWSLambdaPowertoolsPythonV2 Layer]
F --> G
subgraph Lambda Functions
E
F
end
subgraph Agent Action Groups
C
D
end
subgraph Bedrock Foundation Model
B
end
subgraph Layers
G
end
- Python 3
- AWS users with permission to create required resources. See Sample IAM Policy
- Install aws-cdk
- Install Docker
git clone git@github.com:stephenVertex/cdk-lambda-bedrock-template.git
cd cdk-lambda-bedrock-template/
NOTE: Make sure that you change the stack name in app.py
.
CdkLambdaBedrockStack(app, "CdkLambdaBedrockStack", ## <-- Change this to your desired stack name
python3 -m venv .venv
source .venv/bin/activate
and install the dependencies
pip3 install -r requirements.txt
The docker ps
command should work.
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sts:AssumeRole",
"iam:PassRole"
],
"Resource": [
"arn:aws:iam::{ACCT_NUMBER}:role/cdk-hnb659fds-deploy-role-*",
"arn:aws:iam::{ACCT_NUMBER}:role/cdk-hnb659fds-file-publishing-*",
"arn:aws:iam::{ACCT_NUMBER}:role/cdk-hnb659fds-lookup-role-*",
"arn:aws:iam::{ACCT_NUMBER}:role/cdk-readOnlyRole"
]
}
]
}
Note that hnb659fds
is the default value of the all resources in the CDK "bootstrap stack."
If you need to change this, you will know what to do.
Run ./deploy.sh
.
The deployment script makes sure that the API specifications are nice.
cd lambda
python3 app.py > openapi.json
cd ..
cd lambda_two
python3 app.py > openapi.json
cd ..
cdk deploy
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
Enjoy!
The Agents for Bedrock page on AWS Powertools contains much of this same information. I have just made it into one deployable template.
Huge thank you to Darko Mesaroš for helping me wrap my brain around CDK.