Log Amazon Bedrock API calls using AWS CloudTrail - Amazon Bedrock

Log Amazon Bedrock API calls using AWS CloudTrail

Amazon Bedrock is integrated with AWS CloudTrail, a service that provides a record of actions taken by a user, role, or an AWS service in Amazon Bedrock. CloudTrail captures all API calls for Amazon Bedrock as events. The calls captured include calls from the Amazon Bedrock console and code calls to the Amazon Bedrock API operations. If you create a trail, you can enable continuous delivery of CloudTrail events to an Amazon S3 bucket, including events for Amazon Bedrock. If you don't configure a trail, you can still view the most recent events in the CloudTrail console in Event history. Using the information collected by CloudTrail, you can determine the request that was made to Amazon Bedrock, the IP address from which the request was made, who made the request, when it was made, and additional details.

To learn more about CloudTrail, see the AWS CloudTrail User Guide.

Amazon Bedrock information in CloudTrail

CloudTrail is enabled on your AWS account when you create the account. When activity occurs in Amazon Bedrock, that activity is recorded in a CloudTrail event along with other AWS service events in Event history. You can view, search, and download recent events in your AWS account. For more information, see Viewing events with CloudTrail Event history.

For an ongoing record of events in your AWS account, including events for Amazon Bedrock, create a trail. A trail enables CloudTrail to deliver log files to an Amazon S3 bucket. By default, when you create a trail in the console, the trail applies to all AWS Regions. The trail logs events from all Regions in the AWS partition and delivers the log files to the Amazon S3 bucket that you specify. Additionally, you can configure other AWS services to further analyze and act upon the event data collected in CloudTrail logs. For more information, see the following:

Every event or log entry contains information about who generated the request. The identity information helps you determine the following:

  • Whether the request was made with root or AWS Identity and Access Management (IAM) user credentials.

  • Whether the request was made with temporary security credentials for a role or federated user.

  • Whether the request was made by another AWS service.

For more information, see the CloudTrail userIdentity element.

Amazon Bedrock data events in CloudTrail

Data events provide information about the resource operations performed on or in a resource (for example, reading or writing to an Amazon S3 object). These are also known as data plane operations. Data events are often high-volume activities that CloudTrail doesn’t log by default.

Amazon Bedrock doesn't log Amazon Bedrock Runtime API operations (InvokeModel, InvokeModelWithResponseStream, Converse, and ConverseStream).

Amazon Bedrock logs all Agents for Amazon Bedrock Runtime API operations actions to CloudTrail as data events.

  • To log InvokeAgent calls, configure advanced event selectors to record data events for the AWS::Bedrock::AgentAlias resource type.

  • To log Retrieve and RetrieveAndGenerate calls, configure advanced event selectors to record data events for the AWS::Bedrock::KnowledgeBase resource type.

From the CloudTrail console, choose Bedrock agent alias or Bedrock knowledge base for the Data event type. You can additionally filter on the eventName and resources.ARN fields by choosing a custom log selector template. For more information, see Logging data events with the AWS Management Console.

From the AWS CLI, set the resource.type value equal to AWS::Bedrock::AgentAlias or AWS::Bedrock::KnowledgeBase and set the eventCategory equal to Data. For more information, see Logging data events with the AWS CLI.

The following example shows how to configure a trail to log all Amazon Bedrock data events for all Amazon Bedrock resource types in the AWS CLI.

aws cloudtrail put-event-selectors --trail-name trailName \ --advanced-event-selectors \ '[ { "Name": "Log all data events on an Agents for Amazon Bedrock agent alias", "FieldSelectors": [ { "Field": "eventCategory", "Equals": ["Data"] }, { "Field": "resources.type", "Equals": ["AWS::Bedrock::AgentAlias"] } ] }, { "Name": "Log all data events on an Agents for Amazon Bedrock knowledge base", "FieldSelectors": [ { "Field": "eventCategory", "Equals": ["Data"] }, { "Field": "resources.type", "Equals": ["AWS::Bedrock::KnowledgeBase"] } ] } ]'

You can additionally filter on the eventName and resources.ARN fields. For more information about these fields, see AdvancedFieldSelector.

Additional charges apply for data events. For more information about CloudTrail pricing, see AWS CloudTrail Pricing.

Amazon Bedrock management events in CloudTrail

Management events provide information about management operations that are performed on resources in your AWS account. These are also known as control plane operations. CloudTrail logs management event API operations by default.

Amazon Bedrock logs the remainder of Amazon Bedrock API operations as management events. For a list of the Amazon Bedrock API operations that Amazon Bedrock logs to CloudTrail, see the following pages in the Amazon Bedrock API reference.

All Amazon Bedrock API operations and Agents for Amazon Bedrock API operations are logged by CloudTrail and documented in the Amazon Bedrock API Reference. For example, calls to the InvokeModel, StopModelCustomizationJob, and CreateAgent actions generate entries in the CloudTrail log files.

Amazon GuardDuty continuously monitors and analyzes your CloudTrail management and event logs to detect potential security issues. When you enable Amazon GuardDuty for an AWS account, it automatically starts analyzing CloudTrail logs to detect suspicious activity in Amazon Bedrock APIs, such as a user logging in from a new location and using Amazon Bedrock APIs to remove Amazon Bedrock Guardrails, or change the Amazon S3 bucket set for model training data.

Understanding Amazon Bedrock log file entries

A trail is a configuration that enables delivery of events as log files to an Amazon S3 bucket that you specify. CloudTrail log files contain one or more log entries. An event represents a single request from any source and includes information about the requested action, the date and time of the action, request parameters, and so on. CloudTrail log files aren't an ordered stack trace of the public API calls, so they don't appear in any specific order.

The following example shows a CloudTrail log entry that demonstrates the InvokeModel action.

{ "eventVersion": "1.08", "userIdentity": { "type": "IAMUser", "principalId": "AROAICFHPEXAMPLE", "arn": "arn:aws:iam::111122223333:user/userxyz", "accountId": "111122223333", "accessKeyId": "AKIAIOSFODNN7EXAMPLE", "userName": "userxyz" }, "eventTime": "2023-10-11T21:58:59Z", "eventSource": "bedrock.amazonaws.com", "eventName": "InvokeModel", "awsRegion": "us-west-2", "sourceIPAddress": "192.0.2.0", "userAgent": "Boto3/1.28.62 md/Botocore#1.31.62 ua/2.0 os/macos#22.6.0 md/arch#arm64 lang/python#3.9.6 md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.31.62", "requestParameters": { "modelId": "stability.stable-diffusion-xl-v0" }, "responseElements": null, "requestID": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222", "eventID": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111 ", "readOnly": false, "eventType": "AwsApiCall", "managementEvent": true, "recipientAccountId": "111122223333", "eventCategory": "Management", "tlsDetails": { "tlsVersion": "TLSv1.2", "cipherSuite": "cipher suite", "clientProvidedHostHeader": "bedrock-runtime.us-west-2.amazonaws.com" } }