# Saved Logs Insights queries for working a case. They appear under Queries # in the CloudWatch console, in a folder named after var.name. Set the time # range in the console before running one. # # The first three read the invocation records, which cover this Region. The # last two read the trail, which covers every Region. locals { queries = { # AWS's published query: callers by total tokens. tokens-by-caller = { groups = [aws_cloudwatch_log_group.invocations.name] query = <<-EOT fields identity.arn as principal, input.inputTokenCount as inTokens, output.outputTokenCount as outTokens | stats sum(inTokens) as totalInput, sum(outTokens) as totalOutput, count() as calls by principal | sort totalInput desc EOT } # Which principal used which model, and how much. calls-by-model-and-caller = { groups = [aws_cloudwatch_log_group.invocations.name] query = <<-EOT stats count(*) as calls, sum(input.inputTokenCount) as inTokens, sum(output.outputTokenCount) as outTokens by modelId, identity.arn | sort calls desc EOT } # What one caller asked and what came back. @message is the whole # record, prompt and response included. Replace the name, or match on # part of it. requestMetadata is set by the caller, so attribute on # identity.arn only. one-caller = { groups = [aws_cloudwatch_log_group.invocations.name] query = <<-EOT fields @timestamp, operation, modelId, input.inputTokenCount, output.outputTokenCount, @message | filter identity.arn like /REPLACE-WITH-ROLE-OR-USER-NAME/ | sort @timestamp asc EOT } # Key testing. Sysdig saw stolen keys send a model call built to fail: # ValidationException meant the key worked, and AccessDenied meant it # did not. Many refusals from one principal deserve a look. refused-calls-by-caller = { groups = [var.trail_log_group] query = <<-EOT filter eventSource = "bedrock.amazonaws.com" and eventType = "AwsApiCall" | filter errorCode like /AccessDenied/ or errorCode = "ValidationException" | stats count(*) as refused, count_distinct(eventName) as operations by userIdentity.arn, errorCode | sort refused desc EOT } # Calls made with a Bedrock API key, short-term or long-term. CloudTrail # marks them with additionalEventData.callWithBearerToken. bearer-token-calls = { groups = [var.trail_log_group] query = <<-EOT fields @timestamp, userIdentity.arn, eventName, requestParameters.modelId, sourceIPAddress, userAgent, errorCode | filter eventSource = "bedrock.amazonaws.com" | filter @message like /callWithBearerToken.{0,3}true/ | sort @timestamp desc EOT } } } resource "aws_cloudwatch_query_definition" "logging" { for_each = local.queries name = "${var.name}/${each.key}" log_group_names = each.value.groups query_string = each.value.query }