Masking, Redacting and Anonymizing Sensitive Data
This lesson explains how personal or confidential information in training data, RAG indexes and logs is protected during data preparation, since it is hard to remove once a model has learned it or an index stores it.
Learning outcomes
After completing this lesson, you will be able to:
- Discover sensitive data with Macie and Comprehend.
- Choose between redaction, masking, tokenisation and anonymisation.
- Protect personal data at inference time.
Discovering sensitive data
Amazon Macie scans S3 buckets with managed and custom data identifiers and reports objects containing PII, credentials or financial data. Amazon Comprehend detects PII entities (names, addresses, card numbers, IDs) in text and can return their offsets or redacted text. Amazon Comprehend Medical detects protected health information. AWS Glue sensitive data detection finds and masks PII in ETL jobs. Amazon Transcribe redacts PII in transcripts, and Textract output can be passed to Comprehend for redaction.
Find the sensitive data
Protection techniques
Redaction removes the value or replaces it with a type label such as [NAME]. Masking hides part of a value, such as ****-****-****-1234. Tokenization or pseudonymisation replaces values with consistent surrogates, for example a keyed hash (HMAC) of the email address, so records can still be joined without revealing identity. Generalization reduces precision, such as age bands or truncated postal codes, and suppression removes rare combinations that could re-identify people. Unsalted hashes of low-entropy values such as phone numbers can be reversed by brute force, so use a secret key.
The following table summarises the techniques.
| Technique | Example | Keeps joinability | Reversible |
|---|---|---|---|
| Redaction | John Smith -> [NAME] | No | No |
| Masking | 4111 1111 1111 1234 -> ****1234 | No | No |
| Keyed hashing / tokenisation | email -> 8f2c... | Yes | Only with a token vault |
| Generalization | Age 37 -> 30-39 | Partially | No |
The following diagram shows one record from a bank's email fine-tuning dataset before and after personal data is protected.

Mask, tokenise or generalise?
Protection at inference time
Data preparation protects the data used for training and retrieval, but personal data can still enter a system at inference time: users type it into prompts, and models can repeat it in responses. Amazon Bedrock Guardrails sensitive information filters detect personally identifiable information (PII) types, such as names, addresses and card numbers, and custom patterns defined with regular expressions, in both prompts and responses, and either block the request or mask the values.
Logs are another place where personal data accumulates. Bedrock model invocation logs delivered to CloudWatch Logs can use data protection policies, which detect and mask sensitive data in log events, so that logs remain useful for debugging without becoming a privacy risk.
Protecting live traffic
A fine-tuning set from support emails
A bank wants to fine-tune a model on 100,000 customer emails and replies. Analysts must still be able to group emails by customer to keep conversation threads together.
- Macie confirms which S3 prefixes contain account numbers and personal data.
- A Glue job calls Comprehend PII detection, replacing names and addresses with type labels.
- Account numbers are replaced with an HMAC using a key in Secrets Manager, so threads can be grouped but numbers cannot be recovered without the key.
- The deployed application uses a guardrail that masks any account numbers in responses.
Simple masking of account numbers would have broken thread grouping, while plain unsalted hashing would have let anyone with a list of account numbers reverse the hashes.
How can records stay joinable without exposing identities?
Show answer
With keyed hashing or tokenisation, which replace identifiers with consistent values.
Exam Tips
Remember for the exam
- Find PII in S3: Macie; in text: Comprehend; in ETL: Glue sensitive data detection.
- Keep joinability without exposing identity: keyed hashing or tokenisation.
- Redact PII before data reaches training sets or vector indexes.
- Transcribe can redact PII in transcripts.
- Mask PII in prompts and responses at runtime: Guardrails sensitive information filters.