Lesson 5 of 5 Securing AI Service Integrations

Protecting Prompts and Responses in Logs and Traces

This lesson explains which AWS features record the content of AI interactions and how encryption, access control and data protection policies keep that content safe.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify which logging sources contain prompt and response text.
  • Describe how model invocation logging is configured.
  • Explain how KMS encryption and CloudWatch Logs data protection policies protect log data.
  • Describe what application code should and should not log about AI interactions.

What gets logged

Several AWS features record AI interactions, and they differ in how much content they capture. Knowing which ones contain prompt text decides where protection is needed, as the following table shows.

SourceContains prompt and response text?Notes
Bedrock model invocation loggingYes, when enabledOff by default; configured per account and Region
AWS CloudTrailNoRecords the caller, model ID, time and result of each API call
CloudWatch metrics for BedrockNoInvocations, latency, errors and token counts
Application logsOnly if the code logs itThe developer decides what to write
AgentCore Observability tracesCan include prompts and tool inputsSpans and logs stored in CloudWatch
Sort into groups

Does it hold prompt text?

Exam skills 2.3.5

Recognise which AI logs and records can contain prompts and responses.

Log in to try this activity Interactive activities are free with an account.

Model invocation logging

Model invocation logging records the request and response data of every model call in the account and Region, together with metadata, to a CloudWatch Logs log group, an S3 bucket or both. Large payloads that do not fit in a log event are delivered to S3. The configuration chooses which data types are logged, so an application that needs only metadata and text can leave image and embedding data out.

aws bedrock put-model-invocation-logging-configuration --logging-config '{
  "cloudWatchConfig": {
    "logGroupName": "/bedrock/invocations",
    "roleArn": "arn:aws:iam::111122223333:role/BedrockInvocationLogging",
    "largeDataDeliveryS3Config": {"bucketName": "example-bedrock-logs", "keyPrefix": "large/"}
  },
  "textDataDeliveryEnabled": true,
  "imageDataDeliveryEnabled": false,
  "embeddingDataDeliveryEnabled": false
}'

Invocation logs are valuable for auditing, debugging and evaluating quality, but every prompt is copied into them, so they hold the same sensitive data as the application and need the same protection.

Protecting log destinations

The log group and bucket are protected in layers. The log group is encrypted with a customer managed KMS key, associated with aws logs associate-kms-key, whose key policy allows the CloudWatch Logs service to use it and only the security team to decrypt; the bucket uses SSE-KMS and keeps Block Public Access on. IAM limits who can read the logs, and a retention period deletes them when they are no longer needed.

A CloudWatch Logs data protection policy adds masking. It names managed data identifiers, such as email addresses and credit card numbers, or custom ones; matching values are masked for every reader without the logs:Unmask permission, and audit findings can be sent to another log group, Firehose or S3.

{
  "Name": "mask-pii",
  "Version": "2021-06-01",
  "Statement": [
    {"Sid": "audit", "DataIdentifier": ["arn:aws:dataprotection::aws:data-identifier/EmailAddress",
                                        "arn:aws:dataprotection::aws:data-identifier/CreditCardNumber"],
     "Operation": {"Audit": {"FindingsDestination": {}}}},
    {"Sid": "mask", "DataIdentifier": ["arn:aws:dataprotection::aws:data-identifier/EmailAddress",
                                       "arn:aws:dataprotection::aws:data-identifier/CreditCardNumber"],
     "Operation": {"Deidentify": {"MaskConfig": {}}}}
  ]
}

The policy is attached with aws logs put-data-protection-policy. Masking applies when logs are read, so the original data is still stored, encrypted, for principals who are allowed to unmask it.

Scenario questions

Protect logged prompts

Exam skills 2.3.5 · 2.2.6

Practise protecting AI logs that may hold sensitive data.

Log in to try this activity Interactive activities are free with an account.

Application logs and traces

Application code decides what it writes, and the safest default is metadata rather than content. The following practices keep sensitive data out of logs while keeping them useful.

  • Log request IDs, model or profile IDs, token counts, latency, stop reasons and guardrail actions, not full prompts.
  • When content must be logged, mask it first, for example with the Powertools for AWS Lambda data masking utility or the masked output of a guardrail.
  • Apply the same KMS encryption, access control and data protection policies to the log groups that hold AgentCore Observability traces.
  • Keep Kiro enterprise prompt logs, which administrators can enable, in an S3 bucket with a customer managed key and restricted access.
Worked example

An auditable health assistant

A health service must keep a record of every model interaction for audit, but support engineers who read the logs must not see patient details.

The developer enables model invocation logging to a dedicated log group encrypted with a customer managed key, sets a one-year retention period, and attaches a data protection policy that masks names, addresses and health identifiers using managed and custom data identifiers.

Support engineers' roles can read the log group but lack logs:Unmask, so they see masked values; only the compliance team's role has logs:Unmask and kms:Decrypt on the key. The application's own logs record only request IDs, token counts and guardrail actions.

Check yourself

A support engineer can read a log group with a data protection policy. What do they see in masked fields, and what would change that?

Show answer

They see the values masked. Only a principal with the logs:Unmask permission, and decrypt access to the log group's key, can see the original values.

Exam Tips

Remember for the exam

  • Model invocation logging is off by default and stores full prompts and responses when enabled.
  • Mask PII in logs for most readers: a CloudWatch Logs data protection policy; logs:Unmask reveals it.
  • Restrict who can read logged prompts: encrypt with a customer managed KMS key and limit decrypt.
  • CloudTrail records who invoked which model, not the prompt text.