Lesson 8 of 8 Amazon CloudWatch

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.

ClassExamples
PII (personally identifiable information)Names, email addresses, government IDs
PHI (protected health information)Health information linked to a person
Payment dataCard numbers
CredentialsPasswords, API keys

Higher classifications need stronger protection: customer managed KMS keys with restricted policies, field-level encryption where needed, tighter access and shorter retention.

Sort into groups

Classify the data

Exam skills 2.2.6

Assign each kind of data to its category.

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

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.
Scenario questions

Minimise sensitive data

Exam skills 2.2.6

Practise keeping sensitive data out of places that do not need it.

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

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.

ToolWhat it does
CloudWatch Logs data protection policiesDetect and mask data such as emails and card numbers; logs:Unmask is needed to see originals
Powertools data masking utilityErases or masks fields in code before logging
SNS message data protectionMasks 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.

Worked example

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:

LevelAction
Stop it at the sourceRemove the debug statement; log only the last four digits
Mask anything that slips throughA CloudWatch Logs data protection policy masks card numbers on ingestion
Restrict who can see originalsOnly the fraud team's role has logs:Unmask
Clean upDelete 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.

Check yourself

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.