Multi-BU Invoice Reconciliation with AWS Step Functions, AWS Invoicing APIs, Amazon Bedrock, and Amazon DynamoDB
This pattern deploys an AWS Step Functions state machine that uses a Map state to iterate over AWS Invoice Units (business units), fetches invoice summaries for each unit via AWS Invoicing APIs, runs Amazon Bedrock analysis for chargeback allocation, and stores results in an Amazon DynamoDB ledger.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/sfn-invoicing-bedrock-chargeback-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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account 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 installed and configured
- Git Installed
- Node.js 20+ installed
- AWS CDK v2 installed and bootstrapped (
cdk bootstrap) - Amazon Bedrock model access enabled for Anthropic Claude Sonnet in your target region
- At least one AWS Invoice Unit configured in your account
┌────────────────────────────────────────────────────────────────────────────┐
│ AWS Step Functions │
│ │
│ ┌─────────────────┐ ┌──────────────────────────────────────────────┐ │
│ │ ListUnitsTask │───▶│ Map State (parallel per Invoice Unit) │ │
│ │ (AWS Lambda) │ │ │ │
│ │ Invoicing API: │ │ ┌─────────────────┐ ┌──────────────────┐ │ │
│ │ ListInvoiceUnits│ │ │ FetchInvoicesTask│─▶│ AnalysisTask │ │ │
│ └─────────────────┘ │ │ (AWS Lambda) │ │ (AWS Lambda) │ │ │
│ │ │ Invoicing API: │ │ Bedrock Converse │ │ │
│ │ │ ListInvoice │ │ + DynamoDB Write │ │ │
│ │ │ Summaries │ │ │ │ │
│ │ └─────────────────┘ └──────────────────┘ │ │
│ └──────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────┐
│ Amazon DynamoDB │
│ Chargeback Ledger │
└──────────────────┘
-
ListUnitsTask — An AWS Lambda function calls the AWS Invoicing
ListInvoiceUnitsAPI with pagination to discover all business units in the account. -
Map State — AWS Step Functions iterates over each Invoice Unit in parallel (max concurrency 5), processing them independently.
-
FetchInvoicesTask — For each unit, an AWS Lambda function calls
ListInvoiceSummariesfiltered by account, retrieving the last 30 days of invoice data. -
AnalysisTask — An AWS Lambda function sends the invoice data to Amazon Bedrock (Claude Sonnet) for FinOps analysis: total spend calculation, month-over-month trend detection, chargeback allocation recommendations, and cost optimization opportunities. Results are written to Amazon DynamoDB.
-
Amazon DynamoDB Ledger — Stores chargeback allocations per business unit per period, creating an auditable financial record.
-
Clone the repository:
git clone https://github.com/aws-samples/serverless-patterns cd serverless-patterns/sfn-invoicing-bedrock-chargeback-cdk/cdk -
Install dependencies:
npm install
-
Deploy the stack:
cdk deploy
-
(Optional) Use a different model:
cdk deploy --context modelId=us.anthropic.claude-sonnet-4-20250514-v1:0
-
Start a Step Functions execution:
aws stepfunctions start-execution \ --state-machine-arn <StateMachineArn from stack outputs> \ --input '{}'
-
Monitor the execution:
aws stepfunctions describe-execution \ --execution-arn <execution ARN from above>
-
Once complete (status: SUCCEEDED), check the Amazon DynamoDB chargeback ledger:
aws dynamodb scan \ --table-name <ChargebackTableName from stack outputs>
-
Expected result: One item per Invoice Unit with fields including
unitName,invoiceCount,analysis(JSON with totalSpend, trend, chargebackAllocation, optimizations), andanalyzedAttimestamp.
cdk destroyWarning: This deletes the Amazon DynamoDB table and all chargeback history. The table uses
RemovalPolicy.DESTROYfor cleanup convenience. In production, useRETAINor enable point-in-time recovery.
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0