# The record: every model call in this Region, with its prompt and response, # in a CloudWatch log group for searching, and a full copy in S3 for keeping. # Images, documents, and bodies over 100 KB go to the same bucket. Both # stores carry full prompts and responses, so keep read access tight. locals { log_group = "/aws/bedrock/${var.name}" copy_prefix = "invocations" large_data_prefix = "large-data" bedrock_source_arn = "arn:${local.partition}:bedrock:${local.region}:${local.account}:*" } resource "aws_cloudwatch_log_group" "invocations" { name = local.log_group retention_in_days = var.retention_days kms_key_id = aws_kms_key.logging.arn } resource "aws_s3_bucket" "logs" { bucket_prefix = "${var.name}-" force_destroy = false } # Bedrock's bucket policy only takes effect with ACLs disabled. resource "aws_s3_bucket_ownership_controls" "logs" { bucket = aws_s3_bucket.logs.id rule { object_ownership = "BucketOwnerEnforced" } } resource "aws_s3_bucket_public_access_block" "logs" { bucket = aws_s3_bucket.logs.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_versioning" "logs" { bucket = aws_s3_bucket.logs.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_server_side_encryption_configuration" "logs" { bucket = aws_s3_bucket.logs.id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.logging.arn } bucket_key_enabled = true } } # The copy lands under /AWSLogs//BedrockModelInvocationLogs/, # as AWS documents. AWS gives no path for large data objects, so the second # rule assumes they land under their key prefix: check the bucket after the # first image call. resource "aws_s3_bucket_lifecycle_configuration" "logs" { bucket = aws_s3_bucket.logs.id depends_on = [aws_s3_bucket_versioning.logs] rule { id = "expire-invocation-copy" status = "Enabled" filter { prefix = "${local.copy_prefix}/" } expiration { days = var.copy_retention_days } noncurrent_version_expiration { noncurrent_days = 7 } abort_incomplete_multipart_upload { days_after_initiation = 7 } } # Large data lasts as long as the log records that point to it. dynamic "rule" { for_each = var.retention_days > 0 ? [var.retention_days] : [] content { id = "expire-large-data" status = "Enabled" filter { prefix = "${local.large_data_prefix}/" } expiration { days = rule.value } noncurrent_version_expiration { noncurrent_days = 7 } abort_incomplete_multipart_upload { days_after_initiation = 7 } } } } # AWS documents the key path for the copy and gives none for large data # objects, so the write statement covers every object in the bucket. Only # Bedrock calls from this account and Region meet its two conditions. data "aws_iam_policy_document" "bucket" { statement { sid = "AmazonBedrockLogsWrite" actions = ["s3:PutObject"] resources = ["${aws_s3_bucket.logs.arn}/*"] 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] } } statement { sid = "DenyUnencryptedTransport" effect = "Deny" actions = ["s3:*"] principals { type = "AWS" identifiers = ["*"] } resources = [aws_s3_bucket.logs.arn, "${aws_s3_bucket.logs.arn}/*"] condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } } resource "aws_s3_bucket_policy" "logs" { bucket = aws_s3_bucket.logs.id policy = data.aws_iam_policy_document.bucket.json depends_on = [aws_s3_bucket_public_access_block.logs, aws_s3_bucket_ownership_controls.logs] } # The role Bedrock assumes to write to the log group, scoped as AWS documents # it: one log stream, and only Bedrock acting for this account and Region. data "aws_iam_policy_document" "delivery_assume" { statement { actions = ["sts:AssumeRole"] 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_iam_role" "delivery" { name = "${var.name}-delivery" assume_role_policy = data.aws_iam_policy_document.delivery_assume.json } resource "aws_iam_role_policy" "delivery" { name = "delivery" role = aws_iam_role.delivery.id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["logs:CreateLogStream", "logs:PutLogEvents"] Resource = "${aws_cloudwatch_log_group.invocations.arn}:log-stream:aws/bedrock/modelinvocations" }] }) } # One configuration per account and Region; define it in one configuration # only, or applies overwrite each other. Bedrock checks the role against the # log group when it is set, and the provider retries while a new role # propagates. resource "aws_bedrock_model_invocation_logging_configuration" "this" { logging_config { # The provider turns all four on when they are left out. It has no flag # for audio, so audio logging is whatever the API applies when the # setting is absent; AWS documents no default. text_data_delivery_enabled = true image_data_delivery_enabled = var.log_image_data embedding_data_delivery_enabled = var.log_embedding_data video_data_delivery_enabled = var.log_video_data cloudwatch_config { log_group_name = aws_cloudwatch_log_group.invocations.name role_arn = aws_iam_role.delivery.arn large_data_delivery_s3_config { bucket_name = aws_s3_bucket.logs.id key_prefix = local.large_data_prefix } } s3_config { bucket_name = aws_s3_bucket.logs.id key_prefix = local.copy_prefix } } depends_on = [ aws_iam_role_policy.delivery, aws_s3_bucket_policy.logs, aws_s3_bucket_server_side_encryption_configuration.logs, ] }