Skip to main content
CloudWatch Logs includes a built-in query engine called Logs Insights (also known as Log Analytics) that lets you interrogate and extract structured information from raw log events using an SQL-like query language. Logs Insights makes it easy to locate timestamps, event types, model identifiers, latency metrics, and other fields across large volumes of Bedrock invocation logs—much faster and more precise than manually scanning raw log lines.
Logs Insights uses an SQL-like syntax but is not full SQL. To write effective queries, know your log schema (the names and types of fields your application and Bedrock emit).

Common Logs Insights queries for Bedrock model invocations

Below are practical queries and patterns to analyze Bedrock invocation logs (for example, the log group that receives Bedrock model invocation events).
  • Basic extraction of timestamp, event type, and model ID (sorted by timestamp desc, limited to 10,000 results):
When you run a query like the one above, Logs Insights shows a time-series visualization of event counts for the selected time range (for example, last hour). That visualization helps you spot traffic spikes or quiet periods. Under the chart you’ll find the matching log events; expand a log event to inspect the extracted fields.
  • Filter for failed Bedrock requests (show timestamp, event, modelId, durationMs, requestId; most recent 20):
  • Find slow successful responses (duration greater than 3000 ms):
  • Average latency by model (helpful to compare model performance):
  • Approximate token / context window usage (prompt length + output length), sorted descending:
These queries help answer operational questions such as:
  • Which models are slowest on average?
  • Which requests are failing, and when?
  • Are any requests approaching a model’s context window limit?

Quick reference: event names and their purpose

Application-level logging (Lambda)

Bedrock model invocation logs capture model-level input/output, but your application should emit structured logs that provide context around those calls—what the app was doing, which user or API request triggered the model call, and correlation identifiers. For AWS Lambda, anything written to stdout is automatically forwarded to the function’s CloudWatch Logs group. Using a structured logger (JSON) makes logs easier to parse and correlate with model invocation logs.
A slide titled "Workflow: App Logging in Lambda" showing three colorful arrows that illustrate app logging flow from the application and the Lambda Python runtime into a central stdout path. The diagram notes that stdout is automatically sent to CloudWatch logs and suggests enhancing output formatting with a logging module.
Below is a complete Python Lambda handler that:
  • Calls Bedrock via the runtime client,
  • Emits structured JSON events to stdout so they appear in the function’s CloudWatch Logs group, and
  • Includes the Lambda context.aws_request_id so you can correlate application logs with Bedrock model logs.
Do not log sensitive data (PII, credentials, full user messages, or API keys). Use redaction and minimize sensitive fields in logs. Structured logs still carry risk if they include raw user content.

Notes and best practices

  • Use structured logs (JSON) for application events—this makes Logs Insights filters simple and reliable.
  • Include a correlation identifier such as context.aws_request_id so you can map a Lambda invocation’s application logs to Bedrock model invocation logs.
  • Use distinct, consistent event names such as bedrock_request_started, bedrock_request_succeeded, and bedrock_request_failed. The example Log Insights queries above filter on those event names.
  • Emit additional contextual fields (user id, API path, feature flag state, deployment stage) when relevant—this metadata speeds troubleshooting and attribution.
  • For most Lambda use cases, writing to stdout is sufficient; CloudWatch Logs will automatically capture those entries. If you need programmatic control, you can instantiate a CloudWatch Logs client with boto3 to create log groups, push events, or manage retention.
  • When parsing model responses, be prepared for different response shapes depending on the model—implement safe fallbacks and defensive parsing as shown in the example.
By combining Bedrock model-level logs with concise, structured application logs, you can craft powerful Logs Insights queries that surface failures, latency hotspots, and usage patterns across users and applications—enabling faster and more accurate troubleshooting.

Watch Video