Skip to content
Open
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
3 changes: 3 additions & 0 deletions sfn-agentcore-bedrock-cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
cdk.out
96 changes: 96 additions & 0 deletions sfn-agentcore-bedrock-cdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Step Functions with Bedrock AgentCore Multi-Agent Orchestration

This workflow deploys an AWS Step Functions state machine that orchestrates multiple Bedrock AgentCore agents in parallel, then aggregates their responses into a unified summary using Amazon Bedrock Converse.

Learn more about this workflow at Step Functions workflows collection: https://serverlessland.com/workflows/sfn-agentcore-bedrock-cdk

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [Node.js 18+](https://nodejs.org/en/download/) installed
* [AWS CDK v2](https://docs.aws.amazon.com/cdk/v2/guide/getting-started.html) installed
* [Amazon Bedrock model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) enabled for your chosen model (default: Claude Sonnet) in your target region
* One or more [Bedrock AgentCore agent runtimes](https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore.html) already deployed and accessible

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/step-functions-workflows-collection
```
1. Change directory to the pattern directory:
```
cd step-functions-workflows-collection/sfn-agentcore-bedrock-cdk
```
1. Install dependencies:
```
npm install
```
1. Deploy the stack with your AgentCore runtime ARNs:
```
cdk deploy \
--parameters AgentRuntimeArns=arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/agent1,arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/agent2 \
--parameters BedrockModelId=us.anthropic.claude-sonnet-4-20250514-v1:0
```
1. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

1. The **Trigger Lambda** receives a prompt and a list of AgentCore runtime ARNs, then starts the Step Functions state machine execution.
2. The **Map state** fans out to invoke each AgentCore agent in parallel. Each iteration calls the **Invoke Agent Lambda**, which uses the bundled `@aws-sdk/client-bedrock-agentcore` SDK to call `InvokeAgentRuntime` and collect the streaming response.
3. After all agents respond, the **Aggregate Lambda** uses Amazon Bedrock Converse (Claude) to synthesize all agent responses into a single coherent summary.
4. The state machine returns the aggregated summary along with the agent count.

### Important Notes

- **Bundled SDK**: The `@aws-sdk/client-bedrock-agentcore` package is not included in the Lambda runtime. The invoke-agent function uses `NodejsFunction` with esbuild bundling to include it.
- **Streaming Response**: `InvokeAgentRuntime` returns a streaming response, which cannot be consumed directly by Step Functions SDK integrations. A Lambda intermediary collects the stream and returns the full response.
- **Enforced Guardrails**: If your account has account-level enforced guardrails configured, all roles that call Bedrock must have `bedrock:ApplyGuardrail` permission.

## Image

![image](./resources/statemachine.png)

## Testing

1. Invoke the trigger function with a prompt and your agent runtime ARNs:
```bash
aws lambda invoke \
--function-name <TriggerFunctionName> \
--payload '{
"prompt": "What are the best practices for building serverless applications?",
"agentRuntimeArns": [
"arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/agent1",
"arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/agent2"
]
}' \
--cli-binary-format raw-in-base64-out \
output.json
```

2. The trigger returns the Step Functions execution ARN. Monitor the execution:
```bash
aws stepfunctions describe-execution \
--execution-arn <executionArn-from-output.json>
```

3. Once the execution completes (status: `SUCCEEDED`), view the output which contains the aggregated summary from all agents.

## Cleanup

1. Delete the stack
```bash
cdk destroy
```
1. Confirm the stack has been deleted
```bash
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'SfnAgentcoreBedrockStack')].StackStatus"
```
----
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
7 changes: 7 additions & 0 deletions sfn-agentcore-bedrock-cdk/bin/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { SfnAgentcoreBedrockStack } from '../lib/sfn-agentcore-bedrock-stack';

const app = new cdk.App();
new SfnAgentcoreBedrockStack(app, 'SfnAgentcoreBedrockStack');
3 changes: 3 additions & 0 deletions sfn-agentcore-bedrock-cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "npx ts-node bin/app.ts"
}
82 changes: 82 additions & 0 deletions sfn-agentcore-bedrock-cdk/example-workflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"title": "Step Functions with Bedrock AgentCore Multi-Agent Orchestration",
"description": "Use AWS Step Functions to orchestrate multiple Bedrock AgentCore agents in parallel via a Map state, then aggregate their responses into a unified summary using Bedrock Converse.",
"language": "TypeScript",
"simplicity": "3 - Application",
"usecase": "",
"type": "Standard",
"diagram": "/resources/statemachine.png",
"videoId": "",
"level": "400",
"framework": "CDK",
"services": ["bedrock", "lambda", "stepfunctions"],
"introBox": {
"headline": "How it works",
"text": [
"This workflow deploys a Step Functions state machine that invokes multiple Bedrock AgentCore agents in parallel using a Map state, then aggregates their responses into a single coherent summary via Amazon Bedrock Converse.",
"The workflow: (1) A trigger Lambda starts the state machine with a prompt and list of agent runtime ARNs, (2) the Map state fans out to invoke each AgentCore agent in parallel via a bundled Lambda intermediary, (3) an aggregate Lambda uses Bedrock Converse to synthesize all agent responses into a unified summary.",
"Key architectural decisions: The @aws-sdk/client-bedrock-agentcore SDK is not available in the Lambda runtime and must be bundled using NodejsFunction. InvokeAgentRuntime returns a streaming response, requiring a Lambda intermediary rather than direct Step Functions SDK integration."
]
},
"testing": {
"headline": "Testing",
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"headline": "Cleanup",
"text": [
"1. Delete the stack: <code>cdk destroy</code>."
]
},
"deploy": {
"text": [
"cdk deploy --parameters AgentRuntimeArns=<comma-separated-arns> --parameters BedrockModelId=<model-id>"
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/step-functions-workflows-collection/tree/main/sfn-agentcore-bedrock-cdk/",
"templateDir": "sfn-agentcore-bedrock-cdk",
"templateFile": "lib/sfn-agentcore-bedrock-stack.ts",
"ASL": "statemachine/statemachine.asl.json"
},
"payloads": [
{
"headline": "",
"payloadURL": ""
}
]
},
"resources": {
"headline": "Additional resources",
"bullets": [
{
"text": "Amazon Bedrock AgentCore Documentation",
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore.html"
},
{
"text": "AWS Step Functions Map State",
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-map-state.html"
},
{
"text": "Amazon Bedrock Converse API",
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference-call.html"
},
{
"text": "The AWS Step Functions Workshop",
"link": "https://catalog.workshops.aws/stepfunctions/en-US"
}
]
},
"authors": [
{
"name": "Nithin Chandran R",
"image": "https://avatars.githubusercontent.com/u/NithinChandranR-AWS",
"bio": "Nithin is a Technical Account Manager at AWS who builds serverless patterns and AI agent orchestration solutions.",
"linkedin": "nithin-chandran-r",
"twitter": ""
}
]
}
162 changes: 162 additions & 0 deletions sfn-agentcore-bedrock-cdk/lib/sfn-agentcore-bedrock-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdaNode from 'aws-cdk-lib/aws-lambda-nodejs';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as path from 'path';

export class SfnAgentcoreBedrockStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const agentRuntimeArnsParam = new cdk.CfnParameter(this, 'AgentRuntimeArns', {
type: 'String',
default: 'arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/placeholder-agent',
description: 'Comma-separated list of AgentCore runtime ARNs',
});

const bedrockModelIdParam = new cdk.CfnParameter(this, 'BedrockModelId', {
type: 'String',
default: 'us.anthropic.claude-sonnet-4-20250514-v1:0',
description: 'Bedrock model ID for aggregation',
});

// Lambda to invoke a single AgentCore agent (bundled SDK for streaming support)
const invokeAgentFn = new lambdaNode.NodejsFunction(this, 'InvokeAgentFn', {
entry: path.join(__dirname, '..', 'src', 'invoke-agent.ts'),
handler: 'handler',
runtime: lambda.Runtime.NODEJS_20_X,
memorySize: 256,
timeout: cdk.Duration.minutes(3),
bundling: {
minify: true,
sourceMap: false,
},
});

invokeAgentFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['bedrock-agentcore:InvokeAgentRuntime'],
resources: ['*'],
}));

// Aggregate Lambda — summarizes multi-agent responses via Bedrock Converse
const aggregateFn = new lambda.Function(this, 'AggregateFn', {
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'aggregate.handler',
code: lambda.Code.fromAsset('src'),
memorySize: 256,
timeout: cdk.Duration.seconds(60),
environment: {
BEDROCK_MODEL_ID: bedrockModelIdParam.valueAsString,
},
});

aggregateFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['bedrock:InvokeModel'],
resources: ['*'],
}));

// Required when account-level enforced guardrails are active
aggregateFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['bedrock:ApplyGuardrail'],
resources: ['*'],
}));

// SFN IAM Role
const sfnRole = new iam.Role(this, 'SfnRole', {
assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
});

sfnRole.addToPolicy(new iam.PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: [invokeAgentFn.functionArn, aggregateFn.functionArn],
}));

// State machine: Map → invoke agents in parallel → aggregate
const definition = {
Comment: 'Orchestrate multiple AgentCore agents in parallel and aggregate responses with Bedrock',
StartAt: 'InvokeAgents',
States: {
InvokeAgents: {
Type: 'Map',
ItemsPath: '$.agentRuntimeArns',
Parameters: {
'agentRuntimeArn.$': '$$.Map.Item.Value',
'prompt.$': '$.prompt',
},
Iterator: {
StartAt: 'CallAgent',
States: {
CallAgent: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke',
Parameters: {
FunctionName: invokeAgentFn.functionArn,
'Payload.$': '$',
},
OutputPath: '$.Payload',
Retry: [
{
ErrorEquals: ['States.TaskFailed'],
IntervalSeconds: 15,
MaxAttempts: 2,
BackoffRate: 2,
},
],
End: true,
},
},
},
ResultPath: '$.agentResults',
Next: 'AggregateResults',
},
AggregateResults: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke',
Parameters: {
FunctionName: aggregateFn.functionArn,
'Payload.$': '$',
},
OutputPath: '$.Payload',
End: true,
},
},
};

const stateMachine = new sfn.CfnStateMachine(this, 'StateMachine', {
definitionString: JSON.stringify(definition),
roleArn: sfnRole.roleArn,
stateMachineType: 'STANDARD',
});

// Trigger Lambda
const triggerFn = new lambda.Function(this, 'TriggerFn', {
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'trigger.handler',
code: lambda.Code.fromAsset('src'),
memorySize: 256,
timeout: cdk.Duration.seconds(60),
environment: {
STATE_MACHINE_ARN: stateMachine.attrArn,
},
});

triggerFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['states:StartExecution'],
resources: [stateMachine.attrArn],
}));

new cdk.CfnOutput(this, 'StateMachineArn', {
value: stateMachine.attrArn,
});

new cdk.CfnOutput(this, 'TriggerFunctionName', {
value: triggerFn.functionName,
});

new cdk.CfnOutput(this, 'TriggerFunctionArn', {
value: triggerFn.functionArn,
});
}
}
23 changes: 23 additions & 0 deletions sfn-agentcore-bedrock-cdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "sfn-agentcore-bedrock-cdk",
"version": "1.0.0",
"bin": {
"app": "bin/app.ts"
},
"scripts": {
"build": "tsc",
"cdk": "cdk"
},
"dependencies": {
"@aws-sdk/client-bedrock-agentcore": "^3.1032.0",
"aws-cdk-lib": "2.180.0",
"constructs": "10.4.2",
"source-map-support": "^0.5.21"
},
"devDependencies": {
"@types/node": "^20.0.0",
"esbuild": "^0.28.0",
"ts-node": "^10.9.0",
"typescript": "~5.4.0"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading