Skip to content

New Workflow: Bedrock Batch with SLA#418

Open
subhaviv wants to merge 15 commits into
aws-samples:mainfrom
subhaviv:main
Open

New Workflow: Bedrock Batch with SLA#418
subhaviv wants to merge 15 commits into
aws-samples:mainfrom
subhaviv:main

Conversation

@subhaviv

Copy link
Copy Markdown

Summary

  • Adds bedrock-batch-with-sla — a Step Functions workflow that runs Amazon Bedrock batch inference as the primary cost-effective path and
    automatically falls back to on-demand inference when an SLA deadline is at risk
  • Uses the callback (task token) pattern to suspend execution at zero compute cost while waiting for the batch job, with the task token
    stored in DynamoDB
  • Uses the saga pattern to ensure a clean terminal state on any failure path
  • Three independent fallback triggers: Step Functions timeout (SLA enforcement), CloudWatch alarm (stuck job detection), and EventBridge
    (terminal job state — Completed, Failed, Expired, PartiallyCompleted)
  • On-demand redrive uses a Distributed Map reading unprocessed records from JSONL in S3 — no intermediate queue or Lambda fan-out
  • IaC in Python CDK, organized into five constructs (Storage, BedrockService, LambdaCompute, Orchestration, Monitoring)
  • Also updates webhook-provider/example-workflow.json with updated author bio and image URL

How it works

S3 input (.jsonl) → Registrar λ → Step Functions
├─ Happy path: batch completes → MergeResults → Success
└─ Fallback path (timeout / alarm / terminal state):
StopBatchJob → ReconcileRecords
└─ RedriveOnDemand (Distributed Map) → MergeResults → Success

Test plan

  • Unit tests: cd test/lambdas && python -m pytest — 5/5 passing
  • Integration test 1: Timeout fallback (short SLA deployment)
  • Integration test 2: Stuck job CloudWatch alarm fallback
  • Integration test 3: Failed job EventBridge fallback
  • CDK deploy succeeds (cdk deploy)
  • CDK destroy succeeds (cdk destroy)

subhaviv and others added 5 commits July 15, 2026 23:24
Implements Bedrock batch inference with automatic on-demand SLA fallback
using the callback (task token) and saga patterns. Includes CDK (Python),
6 Lambda functions, Distributed Map redrive, and integration tests for all
three fallback triggers: timeout, stuck job alarm, and failed job.
Derive ACCOUNT_ID at runtime via aws sts get-caller-identity so the
script works across accounts without leaking credentials in source.
Add PartiallyCompleted to the EventBridge rule filter and explicitly
route it to FallbackEntry in CheckOutcome — without this, a partially
completed job would hang at WaitForBatchCompletion until the SLA timeout.
…tch-with-sla

New workflow: bedrock-batch-with-sla
Comment thread webhook-provider/example-workflow.json
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/statemachine/statemachine.asl.json Outdated
Comment thread bedrock-batch-with-sla/statemachine/statemachine.asl.json Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/orchestration_construct.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/orchestration_construct.py
Comment thread bedrock-batch-with-sla/lambdas/reconcile/index.py Outdated
Comment thread bedrock-batch-with-sla/statemachine/statemachine.asl.json
Comment thread bedrock-batch-with-sla/cdk_constructs/bedrock_service_construct.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/storage_construct.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/storage_construct.py Outdated
Comment thread bedrock-batch-with-sla/lambdas/resume/index.py Outdated
subhaviv and others added 2 commits July 17, 2026 12:20
Co-authored-by: Ben <9841563+bfreiberg@users.noreply.github.com>
Co-authored-by: Ben <9841563+bfreiberg@users.noreply.github.com>
Comment thread bedrock-batch-with-sla/lambdas/resume/index.py Outdated
Comment thread bedrock-batch-with-sla/lambdas/reconcile/index.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/storage_construct.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/lambda_compute_construct.py Outdated
Comment thread bedrock-batch-with-sla/cdk_constructs/storage_construct.py Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/README.md Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/example-workflow.json Outdated
Comment thread bedrock-batch-with-sla/lambdas/merge/index.py Outdated
Comment thread bedrock-batch-with-sla/lambdas/reconcile/index.py Outdated
@subhaviv
subhaviv marked this pull request as draft July 23, 2026 04:09
@bfreiberg

Copy link
Copy Markdown
Contributor

Looks good now, thank you. Can you please mark the PR as ready for review again.

…tion

IAM:
- Scope InvokeModel to inference-profile ARN, StopModelInvocationJob/
  GetModelInvocationJob to model-invocation-job/* instead of *
- Keep CreateModelInvocationJob on * — AWS batch inference permissions
  guide prescribes this; cross-region profiles route to whichever region
  has capacity so the target ARN cannot be predicted at deploy time
  (docs.aws.amazon.com/bedrock/latest/userguide/batch-inference-permissions.html)
- Fix Bedrock service role: add InvokeModel on foundation-model ARNs in all
  destination regions using bedrock:InferenceProfileArn condition to avoid
  hardcoding regions (docs.aws.amazon.com/bedrock/latest/userguide/geographic-cross-region-inference.html)

Inference profile detection:
- Fix model_id.split(".")[0] in ("us","eu","ap") instead of "." in model_id,
  which incorrectly matched base model IDs like anthropic.claude-sonnet-4-6

S3 helpers:
- Move 4 duplicate S3 helper functions from merge/index.py and reconcile/index.py
  into lambdas/common/s3_utils.py; helpers accept s3 client as first param so
  callers control the client (testable via mock)

Other:
- Fix missing model_id argument in BedrockServiceConstruct call in stack.py
- Add AWS_PROFILE_FLAG support to integration test script so no profile names
  or account numbers are hardcoded in the public repo
@subhaviv
subhaviv marked this pull request as ready for review July 23, 2026 15:58

@bfreiberg bfreiberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. Your workflow sample will be merged soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants