Lesson 3 of 5 Preparing Data for Generative AI

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.

Scenario questions

Find the sensitive data

Exam skills 1.2.8 · 4.3.9

Choose the service that finds sensitive data in each place.

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

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.

TechniqueExampleKeeps joinabilityReversible
RedactionJohn Smith -> [NAME]NoNo
Masking4111 1111 1111 1234 -> ****1234NoNo
Keyed hashing / tokenisationemail -> 8f2c...YesOnly with a token vault
GeneralizationAge 37 -> 30-39PartiallyNo

The following diagram shows one record from a bank's email fine-tuning dataset before and after personal data is protected.

Protecting the bank's email fine-tuning set. Comprehend PII detection in a Glue job replaces names and addresses with type labels. Account numbers become an HMAC computed with a key held in Secrets Manager, so conversation threads can still be grouped by customer but numbers cannot be recovered without the key. At inference, a guardrail masks any account numbers in responses.
The record values and hash are illustrative.
Sort into groups

Mask, tokenise or generalise?

Exam skills 1.2.8

Choose the protection technique for each requirement.

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

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.

True or false

Protecting live traffic

Exam skills 1.2.8 · 4.3.9

Check how sensitive data is protected in prompts and responses.

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

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.

Check yourself

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.