# ai-model-logging / aws / t1 "Standard" # # The quick start's record and alarms, with named exceptions. Standard adds # a full copy of every record in S3, and separate keys for the record and the # alert topic. It alarms on changes to the record key, and on model calls by # anyone outside the roles you name. It also adds a guardrail that can be # enforced account-wide, and saved Logs Insights queries for the case. # # Terraform reads the logging configuration on every plan, which is the call # logging-checked watches, so name the roles that plan in # expected_principal_arns. terraform destroy calls # DeleteModelInvocationLoggingConfiguration and ScheduleKeyDeletion, the calls # logging-changed and record-key-changed watch. data "aws_caller_identity" "current" {} data "aws_partition" "current" {} data "aws_region" "current" {} locals { account = data.aws_caller_identity.current.account_id partition = data.aws_partition.current.partition region = data.aws_region.current.region own_topic = var.alert_topic_arn == "" topic_arn = local.own_topic ? aws_sns_topic.alerts[0].arn : var.alert_topic_arn callers = length(var.model_caller_role_arns) > 0 tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) # CloudTrail records a role's calls under its session ARN, # arn:aws:sts:::assumed-role//, so each role ARN is # turned into that form with a wildcard for the session. expected_sessions = [for a in var.expected_principal_arns : "arn:${local.partition}:sts::${split(":", a)[4]}:assumed-role/${reverse(split("/", a))[0]}/*" ] caller_sessions = [for a in var.model_caller_role_arns : "arn:${local.partition}:sts::${split(":", a)[4]}:assumed-role/${reverse(split("/", a))[0]}/*" ] # A != test on a field the event lacks never matches, so an exclusion alone # would hide every event whose userIdentity has no arn, such as a call from # another account. The NOT EXISTS branch keeps those events in. not_expected = length(local.expected_sessions) == 0 ? "" : join("", [ " && (($.userIdentity.arn NOT EXISTS) || (", join(" && ", [for a in local.expected_sessions : "($.userIdentity.arn != \"${a}\")"]), "))", ]) not_callers = length(local.caller_sessions) == 0 ? "" : join("", [ " && (($.userIdentity.arn NOT EXISTS) || (", join(" && ", [for a in local.caller_sessions : "($.userIdentity.arn != \"${a}\")"]), "))", ]) # The event names each detection watches, in one place so the filters and # the saved queries agree. watched = { logging-changed = ["PutModelInvocationLoggingConfiguration", "DeleteModelInvocationLoggingConfiguration"] # Three published intrusions show stolen keys reading this before using # models: Sysdig 2024-05, Permiso 2024-10, and Sysdig 2026-02. The # console's logging settings page reads it too. logging-checked = ["GetModelInvocationLoggingConfiguration"] # Seen in LLMjacking cases. PutFoundationModelEntitlement is console-only, # and AcceptAgreementRequest is the Marketplace step Sysdig saw in # 2026-02; it accepts any Marketplace offer. No published sample shows # either one's event source, so this filter matches on name alone. model-access = ["PutUseCaseForModelAccess", "CreateFoundationModelAgreement", "PutFoundationModelEntitlement", "AcceptAgreementRequest"] # The API's operation names. No published CloudTrail sample shows them, # so the verify drill tests this one. guardrails-weakened = ["UpdateGuardrail", "DeleteGuardrail", "PutEnforcedGuardrailConfiguration", "DeleteEnforcedGuardrailConfiguration"] api-key-created = ["CreateServiceSpecificCredential"] record-key-changed = ["DisableKey", "ScheduleKeyDeletion", "PutKeyPolicy"] # The four calls invocation logging records, plus batch jobs, which AWS's # compromised-key quarantine policy denies beside them. Converse calls # carry their own event name, so InvokeModel alone misses them. unexpected-caller = ["InvokeModel", "InvokeModelWithResponseStream", "Converse", "ConverseStream", "CreateModelInvocationJob"] } any_of = { for k, names in local.watched : k => join(" || ", [for e in names : "($.eventName = \"${e}\")"]) } detections = { logging-changed = { pattern = "{ ($.eventSource = \"bedrock.amazonaws.com\") && (${local.any_of["logging-changed"]})${local.not_expected} }" description = "Bedrock model invocation logging was changed or deleted." } logging-checked = { pattern = "{ ($.eventSource = \"bedrock.amazonaws.com\") && (${local.any_of["logging-checked"]})${local.not_expected} }" description = "Someone read the Bedrock logging configuration, as stolen keys do before using models." } model-access = { pattern = "{ (${local.any_of["model-access"]})${local.not_expected} }" description = "Someone turned on access to a Bedrock model or accepted a Marketplace offer." } guardrails-weakened = { pattern = "{ ($.eventSource = \"bedrock.amazonaws.com\") && (${local.any_of["guardrails-weakened"]})${local.not_expected} }" description = "A Bedrock guardrail or its account-wide enforcement was changed or removed." } # Short-term keys are made on the client and never reach CloudTrail; the # saved query bearer-token-calls finds their use. api-key-created = { pattern = "{ ($.eventSource = \"iam.amazonaws.com\") && (${local.any_of["api-key-created"]}) && ($.requestParameters.serviceName = \"bedrock.amazonaws.com\")${local.not_expected} }" description = "Someone created a long-term Bedrock API key." } # requestParameters.keyId carries whatever the caller passed, a key ID or # an ARN. AWS's sample entries for all three calls put the key ARN in # resources[0].ARN, so that is the field matched: # docs.aws.amazon.com/kms/latest/developerguide/ct-disablekey.html, # ct-schedule-key-deletion.html, and ct-put-key-policy.html. record-key-changed = { pattern = "{ ($.eventSource = \"kms.amazonaws.com\") && (${local.any_of["record-key-changed"]}) && ($.resources[0].ARN = \"${aws_kms_key.logging.arn}\")${local.not_expected} }" description = "The key for the model invocation record was disabled, scheduled for deletion, or given a new policy. Without it the record cannot be written or read." } } # Streaming calls also write a stream-completion event of type # AwsServiceEvent, so only the API call itself is counted. unexpected_caller = "{ ($.eventSource = \"bedrock.amazonaws.com\") && ($.eventType = \"AwsApiCall\") && (${local.any_of["unexpected-caller"]})${local.not_callers} }" # Bedrock emits these for each failed delivery. AWS lists them under # "Across all model IDs", the console's name for the metric with no # dimension, so the alarms name none. delivery = { cloudwatch = "ModelInvocationLogsCloudWatchDeliveryFailure" s3-copy = "ModelInvocationLogsS3DeliveryFailure" large-data = "ModelInvocationLargeDataS3DeliveryFailure" } } # --------------------------------------------------------------------------- # Two keys. The record key encrypts the log group and the bucket. The topic # has a key of its own, so an alarm about the record key can still be # delivered when that key is disabled. # --------------------------------------------------------------------------- data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account}:root"] } } statement { sid = "CloudWatchLogsEncryptsInvocationLog" actions = ["kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Describe*"] resources = ["*"] principals { type = "Service" identifiers = ["logs.${local.region}.amazonaws.com"] } condition { test = "ArnEquals" variable = "kms:EncryptionContext:aws:logs:arn" values = ["arn:${local.partition}:logs:${local.region}:${local.account}:log-group:${local.log_group}"] } } # For the bucket. AWS documents no KMS permission for the log group's # delivery role; the delivery-failing alarms catch it if one is needed. statement { sid = "BedrockWritesBucket" actions = ["kms:GenerateDataKey"] resources = ["*"] principals { type = "Service" identifiers = ["bedrock.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } condition { test = "ArnLike" variable = "aws:SourceArn" values = [local.bedrock_source_arn] } } } resource "aws_kms_key" "logging" { description = "${var.name}: model invocation records" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.key.json } resource "aws_kms_alias" "logging" { name = "alias/${var.name}" target_key_id = aws_kms_key.logging.key_id } # The AWS managed SNS key cannot be changed to let CloudWatch alarms publish, # and alarms publishing to a topic encrypted with it fail without an error # anyone sees. data "aws_iam_policy_document" "alerts_key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account}:root"] } } statement { sid = "CloudWatchAlarmsPublish" actions = ["kms:Decrypt", "kms:GenerateDataKey*"] resources = ["*"] principals { type = "Service" identifiers = ["cloudwatch.amazonaws.com"] } } } resource "aws_kms_key" "alerts" { count = local.own_topic ? 1 : 0 description = "${var.name}: alert topic" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.alerts_key.json } resource "aws_kms_alias" "alerts" { count = local.own_topic ? 1 : 0 name = "alias/${var.name}-alerts" target_key_id = aws_kms_key.alerts[0].key_id } resource "aws_sns_topic" "alerts" { count = local.own_topic ? 1 : 0 name = var.name kms_master_key_id = aws_kms_key.alerts[0].arn } resource "aws_sns_topic_subscription" "email" { for_each = local.own_topic ? toset(var.alert_emails) : toset([]) topic_arn = aws_sns_topic.alerts[0].arn protocol = "email" endpoint = each.value } # --------------------------------------------------------------------------- # Management event alarms, from the organisation's audit trail. The trail's # log group contains every Region's events, so these cover Regions where # invocation logging is off. # --------------------------------------------------------------------------- resource "aws_cloudwatch_log_metric_filter" "detection" { for_each = local.detections name = "${var.name}-${each.key}" log_group_name = var.trail_log_group pattern = each.value.pattern metric_transformation { name = "AIModelLogging-${each.key}" namespace = "Security/AIModelLogging" value = "1" default_value = "0" } lifecycle { precondition { condition = length(each.value.pattern) <= 1024 error_message = "The ${each.key} filter pattern is over CloudWatch's 1,024 character limit. Shorten expected_principal_arns." } } } resource "aws_cloudwatch_metric_alarm" "detection" { for_each = local.detections alarm_name = "${var.name}-${each.key}" alarm_description = "${each.value.description} In Logs Insights on ${var.trail_log_group}, filter on the event names in the ${var.name}-${each.key} metric filter to see who and what." namespace = "Security/AIModelLogging" metric_name = "AIModelLogging-${each.key}" statistic = "Sum" period = 300 evaluation_periods = 1 threshold = 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] depends_on = [aws_cloudwatch_log_metric_filter.detection] } # Model calls and batch jobs by anyone outside model_caller_role_arns, in any # Region the trail records. This alarm needs no invocation logging there. resource "aws_cloudwatch_log_metric_filter" "unexpected_caller" { count = local.callers ? 1 : 0 name = "${var.name}-unexpected-caller" log_group_name = var.trail_log_group pattern = local.unexpected_caller metric_transformation { name = "AIModelLogging-unexpected-caller" namespace = "Security/AIModelLogging" value = "1" default_value = "0" } lifecycle { precondition { condition = length(local.unexpected_caller) <= 1024 error_message = "The unexpected-caller filter pattern is over CloudWatch's 1,024 character limit. Shorten model_caller_role_arns." } } } resource "aws_cloudwatch_metric_alarm" "unexpected_caller" { count = local.callers ? 1 : 0 alarm_name = "${var.name}-unexpected-caller" alarm_description = "A principal outside model_caller_role_arns called a Bedrock model or started a batch job. Run the saved query ${var.name}/refused-calls-by-caller for key testing, and ${var.name}/one-caller to read what was asked in this Region." namespace = "Security/AIModelLogging" metric_name = "AIModelLogging-unexpected-caller" statistic = "Sum" period = 300 evaluation_periods = 1 threshold = 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] depends_on = [aws_cloudwatch_log_metric_filter.unexpected_caller] } resource "aws_cloudwatch_metric_alarm" "delivery_failing" { for_each = local.delivery alarm_name = "${var.name}-delivery-failing-${each.key}" alarm_description = "Bedrock could not deliver model invocation records (${each.value}). Check the delivery role, the bucket policy, the record key, and that the log group and bucket still exist." namespace = "AWS/Bedrock" metric_name = each.value statistic = "Sum" period = 300 evaluation_periods = 1 threshold = 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] }