π Welcome to Amazon Bedrock Flows Templates π
This repository demonstrates how to import, create, invoke and delete Amazon Bedrock Flows using JSON Flow definition files, with examples implemented in Python.
This video demonstrates how to create and use Amazon Bedrock Flows with JSON templates, including basic setup, template creation, and execution of flows.
- Overview
- Prerequisites
- Quick Start
- How To Use
- Repository Structure
- Flow Definition (JSON)
- Deploying Bedrock Flows using CloudFormation
- Usage Examples
- Flow Templates
- Best Practices
- Resources
- Contributing
Amazon Bedrock Flows allows you to define and orchestrate multi-step generative AI workflows. This repository shows how to:
- Create Flows using JSON template definitions
- Import/export Flow templates
- Execute and manage Flows via Python
- Implement common Flow patterns and best practices
- AWS Account with Amazon Bedrock access
- Python 3.10+
- Required packages:
- boto3
- termcolor
- rich
There are two ways to execute (import a Flow) the samples:
# Clone repository
git clone https://github.com/aws-samples/amazon-bedrock-flows-samples.git
cd amazon-bedrock-flows-samples
# Set up virtual environment
python -m venv src/venv
source src/venv/bin/activate # Linux/Mac
# or
.\src\venv\Scripts\activate # Windows
# Install requirements
pip install -r requirements.txt
# Run flow manager
python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?" \
--cleanup- Navigate to
./notebooks/flow_notebook.ipynb - Follow the interactive examples and execute the cells sequentially
- The notebook provides a more interactive experience with detailed explanations and visualizations
The script supports various command-line arguments to customize its behavior. Here's a complete guide to all available options:
python src/bedrock_flow_manager.py [arguments]| Argument | Description | Default | Required |
|---|---|---|---|
| --region | AWS region for Bedrock operations | Environment or 'us-west-2' | No |
| --profile | AWS profile name | Environment or 'default' | No |
| --flow-name | Custom name for the created flow | Template name | No |
| --test-input | Input text(s) for testing the flow | None | No |
| --cleanup | Clean up resources after testing | False | No |
| --templates-dir | Directory containing flow templates | './templates' | No |
| --existing-role | Name of existing IAM role to use | None | No |
- Basic Usage - Uses default settings:
python src/bedrock_flow_manager.py- Specify Region and Profile:
python src/bedrock_flow_manager.py --region us-east-1 --profile development- Custom Flow Name:
python src/bedrock_flow_manager.py --flow-name "MyCustomFlow"- Test with Single Input:
python src/bedrock_flow_manager.py --test-input "Hello, how are you?"- Test with Multiple Inputs (for iterator flows):
python src/bedrock_flow_manager.py --test-input "Input 1" "Input 2" "Input 3"- Use Custom Templates Directory:
python src/bedrock_flow_manager.py --templates-dir "/path/to/templates"- Clean Up After Testing:
python src/bedrock_flow_manager.py --test-input "Test message" --cleanup- Use Existing IAM Role:
python src/bedrock_flow_manager.py --existing-role "MyBedrockRole"- Combined Example:
python src/bedrock_flow_manager.py \
--region us-west-2 \
--profile production \
--flow-name "ProductionFlow" \
--test-input "Test message" \
--cleanup \
--existing-role "ProductionBedrockRole"The script also respects the following environment variables:
-
`AWS_REGION`or`AWS_DEFAULT_REGION` -
`AWS_PROFILE`or`AWS_DEFAULT_PROFILE`
These will be used if no command-line arguments are provided for region or profile.
-
The
`--test-input`argument can accept either a single string or multiple space-separated strings for iterator flows -
When
`--cleanup`is used, all created resources (flow, versions, aliases) are deleted after testing -
If
`--existing-role`is not provided, the script will create a new IAM role with minimal required permissions -
Template directory will be created if it doesn't exist
-
The script will interactively prompt for template selection if multiple templates are available
When creating a new role (without using --existing-role ), the following permissions are granted:
"bedrock:CreateFlow",
"bedrock:UpdateFlow",
"bedrock:GetFlow",
"bedrock:ListFlows",
"bedrock:DeleteFlow",
"bedrock:ValidateFlowDefinition",
"bedrock:CreateFlowVersion",
"bedrock:GetFlowVersion",
"bedrock:ListFlowVersions",
"bedrock:DeleteFlowVersion",
"bedrock:CreateFlowAlias",
"bedrock:UpdateFlowAlias",
"bedrock:GetFlowAlias",
"bedrock:ListFlowAliases",
"bedrock:DeleteFlowAlias",
"bedrock:InvokeFlow",
"bedrock:TagResource",
"bedrock:UntagResource",
"bedrock:ListTagsForResource"
"bedrock:ApplyGuardrail",
"bedrock:InvokeGuardrail",
"bedrock:InvokeModel",
"bedrock:GetCustomModel",
"bedrock:InvokeAgent",
"bedrock:Retrieve",
"bedrock:RetrieveAndGenerate",
"bedrock:GetPrompt",
"bedrock:ListPrompts",
"bedrock:RenderPrompt"
"bedrock:GetAgent",
"bedrock:GetKnowledgeBase",
"bedrock:GetGuardrail",If using an existing role (--existing-role), ensure it has the necessary permissions for your flow operations.
amazon-bedrock-flows-samples/
βββ docs/
β βββ images/
β βββ rag_kb_flow.png
β βββ multi_turn_agent_flow.png
β βββ conditions_flow.png
β βββ prompt_guardrail_flow.png
β βββ iterator_collector_flow.png
β βββ multi_agent_flow.png
βββ notebooks/
β βββ flow_notebook.ipynb
βββ src/
β βββ bedrock_flow_manager.py
βββ templates/
β βββ rag_kb_flow.json
β βββ multi_turn_agent_flow.json
β βββ conditions_flow.json
β βββ prompt_guardrail_flow.json
β βββ iterator_collector_flow.json
β βββ multi-agent_flow.json
βββ .gitignore
βββ CODE_OF_CONDUCT.md
βββ CONTRIBUTING.md
βββ CONTRIBUTORS.md
βββ LICENSE
βββ README.md
βββ RELEASE_NOTES.md
βββ requirements.txt{
"description": "string",
"executionRoleArn": "string",
"name": "string",
"tags": {
"string" : "string"
},
"definition": {
"connections": [
{
"configuration": { ... },
"name": "string",
"source": "string",
"target": "string",
"type": "string"
}
],
"nodes": [
{
"configuration": { ... },
"inputs": [
{
"expression": "string",
"name": "string",
"type": "string"
}
],
"name": "string",
"outputs": [
{
"name": "string",
"type": "string"
}
],
"type": "string"
}
]
}
}You can deploy the Bedrock Flows using the provided CloudFormation template. The template accepts the following parameters:
AccountId: AWS Account IDRoleName: IAM Role Name for Bedrock Flow execution
- Navigate to the
cloudformationdirectory:
cd amazon-bedrock-flows-samples/cloudformation- Deploy the CloudFormation stack using the AWS CLI:
aws cloudformation create-stack --stack-name bedrock-flows-stack --template-body file://bedrock_flows.json --parameters ParameterKey=AccountId,ParameterValue=<ACCOUNT_ID> ParameterKey=RoleName,ParameterValue=<ROLE_NAME>Replace <ACCOUNT_ID> and <ROLE_NAME> with the appropriate values.
- Wait for the stack creation to complete. You can check the status of the stack using the AWS Management Console or the AWS CLI:
aws cloudformation describe-stacks --stack-name bedrock-flows-stack- Once the stack creation is complete, you can find the ID of the created Bedrock Flow in the stack outputs.
In above example, we have created the flow using the FlowDefinition inline. There are also other options to define the flow:
DefinitionString: Provide the definition as a JSON-formatted string in the DefinitionString property. DefinitionS3Location: Provide an Amazon S3 location in the DefinitionS3Location property that matches the FlowDefinition.
If you use the DefinitionString or DefinitionS3Location property, you can use the DefinitionSubstitutions property to define key-value pairs to replace at runtime.
For more details, refer to the AWS CloudFormation documentation
Example CloudFormation Template
Here is an example of the CloudFormation template using the FlowDefinition inline:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Template to create an Amazon Bedrock Flow using FlowDefinition",
"Parameters": {
"AccountId": {
"Type": "String",
"Description": "AWS Account ID"
},
"RoleName": {
"Type": "String",
"Description": "IAM Role Name for Bedrock Flow execution"
}
},
"Resources": {
"BedrockFlow": {
"Type": "AWS::Bedrock::Flow",
"Properties": {
"Name": "SimpleFlow_CFN",
"ExecutionRoleArn": {
"Fn::Sub": "arn:aws:iam::${AccountId}:role/${RoleName}"
},
"Description": "A simple flow deployed using Cloud formation.",
"Definition": {
"Nodes": [
{
"Type": "Input",
"Name": "FlowInputNode",
"Outputs": [
{
"Name": "document",
"Type": "Array"
}
]
},
{
"Type": "Iterator",
"Name": "PromptIterator",
"Inputs": [
{
"Name": "array",
"Type": "Array",
"Expression": "$.data"
}
],
"Outputs": [
{
"Name": "arrayItem",
"Type": "String"
},
{
"Name": "arraySize",
"Type": "Number"
}
]
},
{
"Type": "Prompt",
"Name": "PromptProcessor",
"Configuration": {
"Prompt": {
"SourceConfiguration": {
"Inline": {
"ModelId": "anthropic.claude-3-sonnet-20240229-v1:0",
"TemplateType": "TEXT",
"InferenceConfiguration": {
"Text": {
"Temperature": 0.7,
"TopP": 1
}
},
"TemplateConfiguration": {
"Text": {
"Text": "{{userPrompt}}"
}
}
}
}
}
},
"Inputs": [
{
"Name": "userPrompt",
"Type": "String",
"Expression": "$.data"
}
],
"Outputs": [
{
"Name": "modelCompletion",
"Type": "String"
}
]
},
{
"Type": "Collector",
"Name": "ResponseCollector",
"Inputs": [
{
"Name": "arrayItem",
"Type": "String",
"Expression": "$.data"
},
{
"Name": "arraySize",
"Type": "Number",
"Expression": "$.data"
}
],
"Outputs": [
{
"Name": "collectedArray",
"Type": "Array"
}
]
},
{
"Type": "Output",
"Name": "FlowOutput",
"Inputs": [
{
"Name": "document",
"Type": "Array",
"Expression": "$.data"
}
]
}
],
"Connections": [
{
"Name": "Input_to_Iterator",
"Source": "FlowInputNode",
"Target": "PromptIterator",
"Type": "Data",
"Configuration": {
"Data": {
"SourceOutput": "document",
"TargetInput": "array"
}
}
},
{
"Name": "Iterator_to_Prompt",
"Source": "PromptIterator",
"Target": "PromptProcessor",
"Type": "Data",
"Configuration": {
"Data": {
"SourceOutput": "arrayItem",
"TargetInput": "userPrompt"
}
}
},
{
"Name": "Prompt_to_Collector",
"Source": "PromptProcessor",
"Target": "ResponseCollector",
"Type": "Data",
"Configuration": {
"Data": {
"SourceOutput": "modelCompletion",
"TargetInput": "arrayItem"
}
}
},
{
"Name": "Iterator_to_Collector_Size",
"Source": "PromptIterator",
"Target": "ResponseCollector",
"Type": "Data",
"Configuration": {
"Data": {
"SourceOutput": "arraySize",
"TargetInput": "arraySize"
}
}
},
{
"Name": "Collector_to_Output",
"Source": "ResponseCollector",
"Target": "FlowOutput",
"Type": "Data",
"Configuration": {
"Data": {
"SourceOutput": "collectedArray",
"TargetInput": "document"
}
}
}
]
}
}
}
},
"Outputs": {
"BedrockFlowArn": {
"Description": "The ARN of the Bedrock Flow",
"Value": {
"Ref": "BedrockFlow"
}
}
}
}python src/bedrock_flow_manager.py \
--test-input "What are the key features of Amazon Bedrock?" \
--cleanup# Start a multi-turn conversation
python src/bedrock_flow_manager.py \
--test-input "What does John Doe owe us?" \
--cleanup
# The flow will prompt for additional information as needed:
# - Account details
# - Time period
# - Transaction types# Input for conditional processing from KnowledgeBase
python src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock as per AWS documentation" \
--cleanup# Single input for guardrail flow
python src/bedrock_flow_manager.py \
--flow-name "bedrock-flow-sample-prompt-with-guardrail" \
--test-input "What is Amazon Bedrock?" \
--cleanuppython src/bedrock_flow_manager.py \
--test-input "What is Amazon Bedrock?" "Give code for hello world in rust" "give a simple json for showing hello bedrock flows" \
--cleanup# Input for conditional processing from LLM
python src/bedrock_flow_manager.py \
--test-input "What is the source of Iron (Fe)" \
--cleanup# Input for agent-based processing, always responds with "Hello World"
python src/bedrock_flow_manager.py \
--test-input "Create a marketing plan for a new AWS service" \
--cleanup# Override flow name
python src/bedrock_flow_manager.py \
--test-input "What is AWS?" \
--flow-name "custom-flow-name" \
--cleanup
# Specify region and profile
python src/bedrock_flow_manager.py \
--test-input "What is AWS?" \
--region us-west-2 \
--profile dev-profile \
--cleanup
# Use custom templates directory
python src/bedrock_flow_manager.py \
--test-input "What is AWS?" \
--templates-dir "./custom-templates" \
--cleanupTemplate for integrating and querying knowledge bases:

- RAG (Retrieval Augmented Generation)
- Document Q&A
- Context-aware responses
- Knowledge base search and retrieval
Template demonstrating interactive, stateful conversations:

- Multi-turn dialogues
- Context maintenance
- Progressive information gathering
- Conversational state management
- Dynamic response generation
Template showcasing conditional logic and branching:

- Decision trees
- Response routing
- Error handling
- Dynamic path selection
Template demonstrating the use of prompt nodes with content filtering and guardrails:

- Content moderation and filtering
- Input/output validation
- Safety boundaries and constraints
- Tone and style enforcement
Template for processing multiple inputs and aggregating responses:

- Queued execution
- Response aggregation
- Multi-query handling
Template demonstrating agent-based workflows:

- Multi-agent collaboration
- Task delegation
- Specialized agent routing
- Complex task orchestration
- Use clear, descriptive step names
- Include error handling in Flow definitions
- Validate inputs before Flow execution
- Use environment variables for configuration
- Follow AWS security best practices
π Related Links:
- Amazon Bedrock Flows Documentation
- AWS SDK for Python
- Amazon Bedrock Samples
- Multi-turn conversation with an agent node for Amazon Bedrock Flows
- Amazon Bedrock KnowledgeBase
- Amazon Bedrock Agents
See CONTRIBUTING for more information.
See CONTRIBUTING for more information.
This project is licensed under the MIT-0 License. See the LICENSE file.
Important
Examples in this repository are for demonstration purposes. Ensure proper security and testing when deploying to production environments.