Skip to content

Commit

Permalink
feat(construct): bedrock batch step functions fragment (#928)
Browse files Browse the repository at this point in the history
* feat(construct): bedrock batch step functions fragment

This PR introduces a new L3 construct BedrockBatchSfn that simplifies running batch inference jobs
with Amazon Bedrock using AWS Step Functions. The construct creates a state machine fragment that:

 - Processes multiple input manifests in parallel using Step Functions Map state
 - Creates and monitors Bedrock batch inference jobs asynchronously
 - Handles job completion to continue state machine execution without polling.
 - Manages IAM roles and permissions for secure execution
 - Provides configurable timeout settings (24h-168h range)

This construct helps developers implement batch inference workflows without managing complex
infrastructure code, while following AWS best practices for security and scalability.

---------

Co-authored-by: Alain Krok <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent b4bd5bf commit 982f970
Show file tree
Hide file tree
Showing 33 changed files with 2,681 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ The following constructs are available in the library:
| [Amazon Bedrock Monitoring (Amazon CloudWatch Dashboard)](./src/patterns/gen-ai/aws-bedrock-cw-dashboard/README.md) | Amazon CloudWatch dashboard to monitor model usage from Amazon Bedrock. | Amazon CloudWatch |
| [TXT to SQL](./src/patterns/gen-ai/aws-text-to-sql/README.md) | Leverages generative AI capabilities to facilitate natural language-based SQL query generation. | Amazon Event Bridge, Amazon Bedrock, AWS Lambda, Amazon SQS, AWS Secrets, and database of choice |
| [LlamaIndex Data Loading](./src/patterns/gen-ai/aws-llama-index-data-loader/README.md) | Use LlamaIndex to load data in preparation for generative AI workloads | Amazon ECS Fargate, Amazon SQS, and AWS Systems Manager Parameters |
| [Bedrock Batch Step Functions](./src/patterns/gen-ai/aws-bedrock-batch-stepfn/README.md) | Manage Bedrock model invocation jobs(batch inference) in AWS Step Functions state machines | AWS Step Functions, AWS Lambda, AWS EventBridge, Amazon Bedrock, AWS IAM |

### L2 Constructs

Expand Down
2 changes: 2 additions & 0 deletions apidocs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

- [AossCwDashboard](classes/AossCwDashboard.md)
- [BaseClass](classes/BaseClass.md)
- [BedrockBatchSfn](classes/BedrockBatchSfn.md)
- [BedrockCwDashboard](classes/BedrockCwDashboard.md)
- [ContainerImage](classes/ContainerImage.md)
- [ContentGenerationAppSyncLambda](classes/ContentGenerationAppSyncLambda.md)
Expand All @@ -43,6 +44,7 @@
- [AossCwDashboardProps](interfaces/AossCwDashboardProps.md)
- [AsyncInferenceConfig](interfaces/AsyncInferenceConfig.md)
- [BaseClassProps](interfaces/BaseClassProps.md)
- [BedrockBatchSfnProps](interfaces/BedrockBatchSfnProps.md)
- [BedrockCwDashboardProps](interfaces/BedrockCwDashboardProps.md)
- [CollectionMonitoringProps](interfaces/CollectionMonitoringProps.md)
- [ContainerImageConfig](interfaces/ContainerImageConfig.md)
Expand Down
257 changes: 257 additions & 0 deletions apidocs/classes/BedrockBatchSfn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
[**@cdklabs/generative-ai-cdk-constructs**](../README.md)

***

[@cdklabs/generative-ai-cdk-constructs](../README.md) / BedrockBatchSfn

# Class: BedrockBatchSfn

A state machine fragment that creates a Bedrock Batch Inference Job and waits for its completion.

Input schema:
```
{
"job_name": string, // Required. Name of the batch inference job
"manifest_keys": string[], // Required. List of S3 keys of the input manifest files
"model_id": string // Required. Model ID to use.
}
```

Output schema:
```
{
"status": string, // Required. Status of the batch job. One of: "Completed" or "PartiallyCompleted"
"bucket": string, // Required. S3 bucket where the output is stored
"keys": string[] // Required. Array of S3 keys for the output files
}
```

Error schema:
```
{
"status": string, // Required. Status will be one of: "Failed", "Stopped", or "Expired"
"error": string, // Required. Error code
"cause": string // Required. Error message
}
```

## Extends

- `StateMachineFragment`

## Constructors

### new BedrockBatchSfn()

> **new BedrockBatchSfn**(`parent`, `id`, `props`): [`BedrockBatchSfn`](BedrockBatchSfn.md)
#### Parameters

##### parent

`Construct`

##### id

`string`

##### props

[`BedrockBatchSfnProps`](../interfaces/BedrockBatchSfnProps.md)

#### Returns

[`BedrockBatchSfn`](BedrockBatchSfn.md)

#### Overrides

`sfn.StateMachineFragment.constructor`

## Properties

### endStates

> `readonly` **endStates**: `INextable`[]
The states to chain onto if this fragment is used

#### Overrides

`sfn.StateMachineFragment.endStates`

***

### node

> `readonly` **node**: `Node`
The tree node.

#### Inherited from

`sfn.StateMachineFragment.node`

***

### startState

> `readonly` **startState**: `State`
The start state of this state machine fragment

#### Overrides

`sfn.StateMachineFragment.startState`

## Accessors

### id

#### Get Signature

> **get** **id**(): `string`
Descriptive identifier for this chainable

##### Returns

`string`

#### Inherited from

`sfn.StateMachineFragment.id`

## Methods

### next()

> **next**(`next`): `Chain`
Continue normal execution with the given state

#### Parameters

##### next

`IChainable`

#### Returns

`Chain`

#### Inherited from

`sfn.StateMachineFragment.next`

***

### prefixStates()

> **prefixStates**(`prefix`?): `StateMachineFragment`
Prefix the IDs of all states in this state machine fragment

Use this to avoid multiple copies of the state machine all having the
same state IDs.

#### Parameters

##### prefix?

`string`

The prefix to add. Will use construct ID by default.

#### Returns

`StateMachineFragment`

#### Inherited from

`sfn.StateMachineFragment.prefixStates`

***

### toSingleState()

> **toSingleState**(`options`?): `Parallel`
Wrap all states in this state machine fragment up into a single state.

This can be used to add retry or error handling onto this state
machine fragment.

Be aware that this changes the result of the inner state machine
to be an array with the result of the state machine in it. Adjust
your paths accordingly. For example, change 'outputPath' to
'$[0]'.

#### Parameters

##### options?

`SingleStateOptions`

#### Returns

`Parallel`

#### Inherited from

`sfn.StateMachineFragment.toSingleState`

***

### toString()

> **toString**(): `string`
Returns a string representation of this construct.

#### Returns

`string`

#### Inherited from

`sfn.StateMachineFragment.toString`

***

### isConstruct()

> `static` **isConstruct**(`x`): `x is Construct`
Checks if `x` is a construct.

Use this method instead of `instanceof` to properly detect `Construct`
instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the `constructs` library on
disk are seen as independent, completely different libraries. As a
consequence, the class `Construct` in each copy of the `constructs` library
is seen as a different class, and an instance of one class will not test as
`instanceof` the other class. `npm install` will not create installations
like this, but users may manually symlink construct libraries together or
use a monorepo tool: in those cases, multiple copies of the `constructs`
library can be accidentally installed, and `instanceof` will behave
unpredictably. It is safest to avoid using `instanceof`, and using
this type-testing method instead.

#### Parameters

##### x

`any`

Any object

#### Returns

`x is Construct`

true if `x` is an object created from a class which extends `Construct`.

#### Inherited from

`sfn.StateMachineFragment.isConstruct`
Loading

0 comments on commit 982f970

Please sign in to comment.