Classifying Sensitive Data and Masking It in Logs and Messages
This lesson explains how applications identify which data is sensitive and keep it out of places it does not belong, such as logs, error messages, analytics stores and test environments.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the common data classification categories and how they affect handling.
- Identify practices that minimise sensitive data, such as tokenisation and synthetic test data.
- Describe how CloudWatch Logs data protection policies and other controls mask sensitive data.
Classifying data
Data classification assigns each kind of data to a category that determines how it must be handled. Developers need to recognise the categories because they decide where data may be stored, who may read it and whether it may be logged.
Personally identifiable information (PII) is any data that identifies a person, alone or combined with other data, such as a name, email address, phone number or government ID number. Protected health information (PHI) is health data linked to an individual, which regulations such as HIPAA in the United States protect. Payment data, such as card numbers, is governed by the PCI DSS standard, which strictly limits where it may be stored and displayed. Credentials, such as passwords, API keys and tokens, give access to systems and must never appear in code, logs or error messages. Organisations also define categories such as confidential and public for their own business data. The following table summarises the principal categories.
| Class | Examples |
|---|---|
| PII (personally identifiable information) | Names, email addresses, government IDs |
| PHI (protected health information) | Health information linked to a person |
| Payment data | Card numbers |
| Credentials | Passwords, API keys |
Higher classifications need stronger protection: customer managed KMS keys with restricted policies, field-level encryption where needed, tighter access and shorter retention.
Minimising sensitive data
Sensitive data should be minimised wherever it is stored. The following list describes the recommended practices.
- Store only the data you need.
- Replace sensitive values with tokens where possible.
- Use masked or synthetic data in analytics stores and test environments, never copies of production PII.
Masking data in logs and messages
Sensitive data must not appear in logs or messages in readable form, because logs are kept for a long time and read by many people. AWS provides protection at three points.
A CloudWatch Logs data protection policy, attached to a log group or to the whole account, uses managed data identifiers to detect data such as email addresses, card numbers and credentials as log events are ingested, and masks it in everything that reads the logs, including Logs Insights and subscription filters. The original values are kept, and only principals with the logs:Unmask permission can see them. Because it works at ingestion, it protects against data that code logs by mistake.
The Powertools data masking utility works in the code itself: it erases or encrypts named fields of an event before the event is logged or passed on, so the sensitive values never leave the function. SNS message data protection applies the same idea to messages: a policy on a topic can audit, mask or block messages that contain sensitive data, both when they are published and when they are delivered to particular subscribers. It was closed to new customers on 30 April 2026: accounts that already use it can continue, but new applications mask sensitive fields in code, for example with the Powertools utility, before they publish. The following table summarises the three tools.
| Tool | What it does |
|---|---|
| CloudWatch Logs data protection policies | Detect and mask data such as emails and card numbers; logs:Unmask is needed to see originals |
| Powertools data masking utility | Erases or masks fields in code before logging |
| SNS message data protection | Masks or blocks sensitive data in messages (closed to new customers since April 2026) |
from aws_lambda_powertools.utilities.data_masking import DataMasking
masker = DataMasking()
def handler(event, context):
safe = masker.erase(event, fields=["customer.email", "payment.card_number"])
logger.info("Received order", extra={"order": safe})Error responses should never expose internal details or data.
Card numbers in the logs
A security review finds full card numbers in a payment function's logs, written by a debug statement that logs the whole request. The team responds at four levels:
| Level | Action |
|---|---|
| Stop it at the source | Remove the debug statement; log only the last four digits |
| Mask anything that slips through | A CloudWatch Logs data protection policy masks card numbers on ingestion |
| Restrict who can see originals | Only the fraud team's role has logs:Unmask |
| Clean up | Delete the affected log streams after confirming retention requirements |
Classifying data first (card numbers are highly sensitive) decides how strictly it must be handled. Minimising what is collected is always the strongest control.
Which CloudWatch Logs feature masks sensitive data, such as card numbers, in log events?
Show answer
A data protection policy on the log group. It finds the data types it is configured for and masks them, and only principals with the logs:Unmask permission can see the original values.
Exam Tips
Remember for the exam
- To mask PII in logs automatically, use a CloudWatch Logs data protection policy.
- To keep sensitive data out of SNS messages in a new application, mask it in code before publishing; SNS message data protection is closed to new customers.
- Protect highly sensitive data with a customer managed KMS key and restricted access.
- Do not copy production PII into test environments; mask or synthesise the data instead.