Skip to content

Commit 9a946cb

Browse files
committed
feat: Add comprehensive CloudWatch dashboard for monitoring
Implements a complete CloudWatch dashboard with custom Lambda-powered widgets for real-time monitoring and visualization of usage metrics. The dashboard provides comprehensive insights into token consumption, user activity, code effectiveness, and cost tracking. Key Features: - DynamoDB single-table design for efficient metrics storage - 10+ custom dashboard widgets with responsive layouts - Real-time token usage and cost tracking - User activity monitoring and analytics - Code acceptance rate tracking - Cache efficiency metrics - Operations breakdown by type - Lambda layer architecture for shared utilities Technical Implementation: - EventBridge scheduled metrics aggregation every 5 minutes - Lambda functions for custom widget rendering - DynamoDB TTL for automatic 30-day data retention - Support for 10,000+ users with sub-second response times
1 parent 18e6c8d commit 9a946cb

File tree

36 files changed

+6988
-757
lines changed

36 files changed

+6988
-757
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ parameters.json
4949
source/.ccwb-config/
5050

5151
guidance-submission/
52-
tests/
52+
tests/
53+
assets/docs/planning
54+
55+
.pytest_cache/
56+
.ruff_cache/

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ This guidance implements an Enterprise Authentication Pattern that enables organ
8787
- **Temporary AWS Credentials**: Eliminates long-lived credentials by providing session-based access to Amazon Bedrock
8888
- **Centralized Access Control**: Manage Claude Code access through your existing identity provider groups and policies
8989
- **Comprehensive Audit Logging**: Full CloudTrail integration for compliance and security monitoring
90-
- **Optional Usage Monitoring**: CloudWatch dashboards and metrics for tracking Claude Code usage across your organization
90+
- **Optional Usage Monitoring**: CloudWatch dashboards with metrics for tracking Claude Code usage, costs, and performance across your organization
9191

9292
Step-by-step flow for end-users:
9393

@@ -172,6 +172,7 @@ The guidance can be deployed in any AWS region that supports:
172172
### Cross-Region Inference
173173

174174
Claude Code uses Amazon Bedrock's cross-region inference for optimal performance and availability. During setup, you can:
175+
175176
- Select your preferred Claude model (Opus, Sonnet, Haiku)
176177
- Choose a cross-region profile (US, Europe, APAC) for optimal regional routing
177178
- Select a specific source region within your profile for model inference
@@ -220,10 +221,12 @@ This creates the following AWS resources:
220221
- ECS Fargate cluster running OpenTelemetry collector
221222
- Application Load Balancer for OTLP ingestion
222223
- CloudWatch Log Groups and Metrics
223-
- CloudWatch Dashboard with usage analytics
224-
- Kinesis Data Firehose for streaming metrics to S3
225-
- Amazon Athena for SQL analytics on collected metrics
226-
- S3 bucket for long-term metrics storage
224+
- CloudWatch Dashboard with comprehensive usage analytics
225+
- DynamoDB table for metrics aggregation and storage
226+
- Lambda functions for custom dashboard widgets
227+
- Kinesis Data Firehose for streaming metrics to S3 (if analytics enabled)
228+
- Amazon Athena for SQL analytics on collected metrics (if analytics enabled)
229+
- S3 bucket for long-term metrics storage (if analytics enabled)
227230

228231
### Step 3: Create Distribution Package
229232

@@ -326,10 +329,11 @@ Claude Code will automatically use your organization's authentication to access
326329

327330
The optional CloudWatch dashboard provides:
328331

329-
- **Usage Metrics**: Requests per user, model, and region
330-
- **Token Consumption**: Input/output tokens with cost estimation
331-
- **Performance Metrics**: Response times and error rates
332-
- **User Activity**: Active users and authentication patterns
332+
- **Token Consumption**: Input, output, and cache tokens tracked by user, model, and type
333+
- **Code Activity**: Lines of code written vs accepted, showing Claude Code effectiveness
334+
- **User Activity**: Active users, top consumers, and usage patterns throughout the day
335+
- **Cache Performance**: Cache hit rates and token savings from prompt caching
336+
- **Operations Breakdown**: Distribution of Claude Code operations (file edits, searches, reads, etc.)
333337

334338
## Troubleshooting
335339

@@ -374,13 +378,6 @@ The guidance includes a comprehensive CLI tool (`ccwb`) for deployment and manag
374378
- [Monitoring and Telemetry Guide](/assets/docs/MONITORING.md) - Guide to deploying and using Claude Code Telemetry with OpenTelemetry
375379
- [Analytics Guide](/assets/docs/ANALYTICS.md) - Advanced analytics with Kinesis Firehose, S3 data lake, and Athena SQL queries
376380

377-
**OIDC Provider Setup Guides:**
378-
379-
- [Okta Setup](/assets/docs/providers/okta-setup.md)
380-
- [Microsoft Entra ID (Azure AD) Setup](/assets/docs/providers/microsoft-entra-id-setup.md)
381-
- [Auth0 Setup](/assets/docs/providers/auth0-setup.md)
382-
- [Cognito User Pool Setup](/assets/docs/providers/cognito-user-pool-setup.md)
383-
384381
### Supported OIDC Providers
385382

386383
Detailed setup guides are available for:
@@ -408,18 +405,21 @@ poetry run pre-commit install
408405
#### How It Works
409406

410407
When you commit changes, the following validations run automatically:
408+
411409
- **YAML validation** checks syntax in all `.yaml` files
412410
- **CloudFormation validation** checks template structure and properties
413411
- **AWS CLI validation** validates templates against AWS specifications (if credentials are configured)
414412

415413
The validation automatically catches:
414+
416415
- YAML syntax errors that would cause deployment failures
417416
- CloudFormation template structure problems
418417
- Missing required parameters or invalid resource configurations
419418

420419
#### Manual Validation (Optional)
421420

422421
To run validation without committing:
422+
423423
```bash
424424
cd source
425425
poetry run pre-commit run --all-files

assets/docs/CLI_REFERENCE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ The Claude Code with Bedrock CLI (`ccwb`) provides commands for IT administrator
3030

3131
```bash
3232
# Clone the repository
33-
git clone <repository-url>
34-
cd claude-code-auth-setup/source
33+
git clone [<repository-url>](https://github.com/aws-solutions-library-samples/guidance-for-claude-code-with-amazon-bedrock.git)
34+
cd guidance-for-claude-code-with-amazon-bedrock/source
3535

3636
# Install dependencies
3737
poetry install

assets/docs/MONITORING.md

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ When you enable monitoring during deployment, the system creates infrastructure
88

99
The monitoring system consists of several components working together. Claude Code sends metrics using the OpenTelemetry Protocol (OTLP) to an Application Load Balancer. The ALB forwards these metrics to an OTEL Collector running on ECS Fargate. The collector then converts the metrics to CloudWatch's Embedded Metric Format (EMF) and sends them to CloudWatch Metrics and Logs. Finally, a CloudWatch Dashboard visualizes these metrics.
1010

11-
The OTEL Collector serves as a critical bridge because CloudWatch doesn't support OTLP metrics directly. Claude Code uses the industry-standard OTLP format, while CloudWatch requires its proprietary EMF format. The collector handles this translation, ensuring metrics flow smoothly from Claude Code to your CloudWatch dashboards.
12-
1311
## Implementation Details
1412

1513
The monitoring infrastructure deploys several AWS resources to handle metric collection and visualization.
@@ -18,7 +16,7 @@ The core component runs as an ECS Fargate service using the AWS Distro for OpenT
1816

1917
An Application Load Balancer sits in front of the ECS service, receiving OTLP metrics on port 4318. The ALB supports both HTTP and HTTPS protocols. When you provide a custom domain name during deployment, the system automatically creates an ACM certificate and configures HTTPS. Health checks monitor the collector's availability through the root endpoint.
2018

21-
The CloudWatch Dashboard provides comprehensive visualization of your Claude Code usage. It displays metrics by user, tracks token consumption for both input and output, shows request counts broken down by model, monitors error rates and latency, and includes cost estimation widgets to help track spending.
19+
The CloudWatch Dashboard provides comprehensive visualization of your Claude Code usage. The dashboard uses Lambda functions and DynamoDB for efficient metrics collection and display, presenting real-time and historical usage data through custom widgets.
2220

2321
### Configuration
2422

@@ -56,14 +54,6 @@ The deployment creates the complete monitoring infrastructure: a VPC with public
5654

5755
If you provide a custom domain name and hosted zone ID during setup, the system automatically provisions an ACM certificate and configures HTTPS. This ensures encrypted transmission of metrics from Claude Code to your collector.
5856

59-
After deployment completes, the package command handles all the configuration needed for end users:
60-
61-
```bash
62-
poetry run ccwb package
63-
```
64-
65-
This creates a distribution folder containing the credential process executable, configuration files, installation script, Claude Code telemetry settings, and platform-specific OTEL helper binaries that extract user attributes from authentication tokens.
66-
6757
## Claude Code Configuration
6858

6959
The package command generates a `.claude/settings.json` file that configures Claude Code for telemetry collection. This file gets installed to the user's home directory and contains all the settings needed for monitoring.
@@ -105,13 +95,8 @@ Organizational dimensions provide additional context. The `department` field gro
10595

10696
The CloudWatch Dashboard named `ClaudeCodeMonitoring` provides comprehensive visualization of your Claude Code metrics.
10797

108-
The dashboard displays token usage metrics in multiple formats. Total token usage appears as time series graphs showing 5-minute and hourly intervals. Token breakdown by type (input, output, cache creation, cache read) helps you understand usage patterns. The dashboard includes pie charts showing token distribution over 30-day periods and stacked time series for analyzing trends.
109-
110-
Cost tracking features estimate expenses based on token consumption. The dashboard calculates hourly costs using the configurable token price parameter (default $15 per million tokens). Single value widgets show today's cost, this week's cost, and this month's total cost. Time series graphs track cost trends with budget threshold annotations.
111-
112-
Session and activity metrics track active sessions per hour, helping you understand usage patterns throughout the day. The dashboard also monitors Bedrock API calls broken down by model (Sonnet, Haiku, Opus), showing which models your users prefer.
113-
114-
For detailed user analytics, the dashboard includes a section that directs you to AWS Athena. While the dashboard shows aggregate metrics, Athena queries enable user-specific analysis including top users by token consumption, usage by department or team, and cost allocation by user. The system stores pre-built queries in SSM Parameter Store for common analytics tasks.
98+
![Claude Code Monitoring Dashboard](/assets/images/ClaudeCodeDashboard.png)
99+
_Full dashboard view showing all metrics_
115100

116101
## End User Experience
117102

@@ -131,17 +116,6 @@ CloudTrail tracking captures every Bedrock model invocation, storing detailed lo
131116

132117
The monitoring dashboard includes several cost-related features. A separate Bedrock cost dashboard tracks AWS Billing charges for the Bedrock service. The main dashboard estimates costs based on token usage with configurable pricing (default $15 per million tokens). Real-time cost widgets show today's cost, this week's cost, and this month's total spending.
133118

134-
The system includes pre-configured CloudWatch alarms for cost control:
135-
136-
- **High Token Usage**: Alerts when usage exceeds 10 million tokens per hour
137-
- **Daily Token Threshold**: Triggers when daily usage exceeds approximately $1000 worth of tokens (66.7 million tokens at default pricing)
138-
- **High Bedrock Usage**: Alerts when Bedrock API calls exceed 1000 per hour
139-
- **Unusual Model Usage**: Monitors expensive model usage, alerting when it exceeds 50 million tokens per hour
140-
141-
Bedrock tracking is enabled by default in the authentication stack. To disable it during deployment, set the CloudFormation parameter `EnableBedrockTracking` to false.
142-
143-
The monitoring system tracks costs through multiple mechanisms. Token-based estimation provides immediate feedback on usage costs. The Bedrock cost dashboard shows actual AWS Billing charges for the Bedrock service. CloudWatch alarms help prevent unexpected spending by alerting on unusual usage patterns.
144-
145119
### Data Privacy
146120

147121
The system collects only usage metrics, never capturing or transmitting conversation content between users and Claude. User attribution works through the email address from the OIDC token, providing clear accountability without excessive data collection. CloudWatch retains metrics for 15 months by default, though you can adjust this based on your retention policies. Beyond the email address used for attribution, no personally identifiable information is transmitted or stored.

assets/docs/providers/auth0-setup.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This guide walks you through setting up Auth0 from scratch to work with the AWS Cognito Identity Pool for Bedrock access.
44

55
## Table of Contents
6+
67
1. [Create Auth0 Account](#1-create-auth0-account)
78
2. [Access Dashboard](#2-access-dashboard)
89
3. [Create Native Application](#3-create-native-application)
@@ -60,6 +61,7 @@ If you don't have an Auth0 account:
6061
### Step 4.1: Note the Client ID
6162

6263
After creation, you'll see:
64+
6365
- **Client ID**: Something like `aBcDeFgHiJkLmNoPqRsTuVwXyZ123456`
6466
- **Domain**: Your tenant domain like `your-name.auth0.com`
6567

@@ -106,6 +108,7 @@ Click **Save Changes** at the bottom of the page
106108
### Step 5.2: Create a Test User
107109

108110
Fill in the form:
111+
109112
- **Email**: `[email protected]`
110113
- **Password**: Enter a secure password
111114
- **Repeat Password**: Confirm the password
@@ -116,6 +119,7 @@ Click **Create**
116119
### Step 5.3: Create Additional Users (Optional)
117120

118121
Repeat to create more test users:
122+
119123
120124
121125

@@ -136,6 +140,7 @@ By default, all users have access to all applications in Auth0. To restrict acce
136140
### Step 6.2: Enable Organizations (Optional)
137141

138142
For enterprise deployments:
143+
139144
1. Go to **Organizations**
140145
2. Create an organization
141146
3. Add users to the organization
@@ -147,10 +152,10 @@ For enterprise deployments:
147152

148153
You now have everything needed for deployment:
149154

150-
| Parameter | Your Value | Example |
151-
|-----------|------------|---------|
152-
| **Auth0Domain** | Your Auth0 domain | `your-name.auth0.com` |
153-
| **Auth0ClientId** | Your Client ID | `aBcDeFgHiJkLmNoPqRsTuVwXyZ123456` |
155+
| Parameter | Your Value | Example |
156+
| ----------------- | ----------------- | ---------------------------------- |
157+
| **Auth0Domain** | Your Auth0 domain | `your-name.auth0.com` |
158+
| **Auth0ClientId** | Your Client ID | `aBcDeFgHiJkLmNoPqRsTuVwXyZ123456` |
154159

155160
### Use the values with ccwb init
156161

@@ -202,20 +207,24 @@ Should return a JSON response with OIDC endpoints.
202207
## Troubleshooting
203208

204209
### "Invalid redirect URI" Error
210+
205211
- Ensure the callback URL is exactly: `http://localhost:8400/callback`
206212
- No trailing slashes or HTTPS
207213

208214
### "Unauthorized" Error
215+
209216
- Check if user exists and password is correct
210217
- Verify application is active
211218
- Check for any Rules or Actions blocking access
212219

213220
### Can't Find Client ID
221+
214222
1. Go to **Applications****Applications**
215223
2. Click on your application
216224
3. Client ID is at the top of the Settings tab
217225

218226
### Token Issues
227+
219228
- Ensure Authorization Code grant type is enabled
220229
- Check that PKCE is not explicitly disabled
221230
- Verify refresh token settings
@@ -228,7 +237,7 @@ Once you've completed this Auth0 setup:
228237

229238
1. Clone the repository:
230239
```bash
231-
git clone https://gitlab.aws.dev/schuettc/claude-code-setup.git
240+
git clone https://github.com/aws-solutions-library-samples/guidance-for-claude-code-with-amazon-bedrock.git
232241
cd claude-code-setup
233242
poetry install
234243
```
@@ -242,12 +251,14 @@ Once you've completed this Auth0 setup:
242251
## Security Best Practices
243252

244253
1. **Production Considerations**:
254+
245255
- Enable MFA for all users
246256
- Use Auth0 Organizations for enterprise deployments
247257
- Set appropriate session and token lifetimes
248258
- Monitor logs regularly
249259

250260
2. **Token Settings**:
261+
251262
- Enable refresh token rotation
252263
- Set token expiration to 8 hours or less
253264
- PKCE is automatically enabled for native apps
@@ -265,6 +276,7 @@ Once you've completed this Auth0 setup:
265276
### Custom Domain
266277

267278
For production environments:
279+
268280
1. Go to **Settings****Custom Domains**
269281
2. Add your domain (e.g., `auth.company.com`)
270282
3. Verify DNS settings
@@ -273,19 +285,24 @@ For production environments:
273285
### Add Custom Claims
274286

275287
To include user metadata in tokens:
288+
276289
1. Go to **Actions****Flows****Login**
277290
2. Create a custom Action
278291
3. Add claims to the ID token:
279292
```javascript
280293
exports.onExecutePostLogin = async (event, api) => {
281294
api.idToken.setCustomClaim('email', event.user.email);
282-
api.idToken.setCustomClaim('department', event.user.user_metadata.department);
295+
api.idToken.setCustomClaim(
296+
'department',
297+
event.user.user_metadata.department,
298+
);
283299
};
284300
```
285301

286302
### Enable Enterprise Connections
287303

288304
For SSO with corporate identity providers:
305+
289306
1. Go to **Authentication****Enterprise**
290307
2. Choose your connection type (SAML, OIDC, etc.)
291308
3. Configure according to your IdP requirements
@@ -300,4 +317,4 @@ For SSO with corporate identity providers:
300317
- Users: `https://manage.auth0.com/dashboard/users`
301318
- Logs: `https://manage.auth0.com/dashboard/logs`
302319

303-
Remember to navigate to the correct tenant if you have multiple!
320+
Remember to navigate to the correct tenant if you have multiple!

0 commit comments

Comments
 (0)