Skip to content

How do I protect sensitive data in API Gateway execution logs without disabling data tracing?

6 minute read
Content level: Intermediate
0

I want to mask sensitive data such as API keys, tokens, and personally identifiable information (PII) in my Amazon API Gateway execution logs in Amazon CloudWatch Logs (CloudWatch Logs) without turning off data tracing.

Short description

When you turn on data tracing for API Gateway in CloudWatch Logs, the service logs full request and response payloads, including sensitive values such as API keys, authorisation tokens, and PII. To mask this data while keeping troubleshooting visibility, use CloudWatch Logs data protection policies. These policies automatically detect and mask sensitive data in log events using AWS managed data identifiers or custom regex patterns.

Resolution

Important: Full payload logging from data tracing can expose sensitive data in your logs. Before you turn on data tracing in production, review your organisation's data classification and compliance requirements.

How data protection works

CloudWatch Logs data protection uses data identifiers to detect sensitive data patterns in log events. When the service finds a match, it masks the data with asterisks in the log output. There are two types of data identifiers.

Data identifier typeDescriptionUse it for
Managed data identifiersPre-built patterns that AWS provides for common sensitive data types.Credit card numbers, Social Security numbers, email addresses, IP addresses, and names.
Custom data identifiersRegex patterns that you define for organisation-specific sensitive data.Internal tokens, custom API keys, and proprietary identifiers.

Note: Masking does not delete the original data. It hides the sensitive values when someone views the logs. Team members who hold the logs:Unmask IAM permission view the original data when they need it for debugging.

Prerequisites

Before you begin, make sure that you have the following:

  • An API Gateway REST API with data tracing turned on.
  • A CloudWatch Logs execution log group for your API (format: API-Gateway-Execution-Logs_{api-id}/{stage-name}).
  • IAM permissions to create data protection policies (logs:PutDataProtectionPolicy).

Option 1: Create an account-wide data protection policy

Use this option to apply consistent data protection across all CloudWatch log groups in your account.

Complete the following steps:

  1. Open the Amazon CloudWatch console.
  2. In the navigation pane, choose Settings.
  3. Choose the Logs tab.
  4. In the Data protection account policy section, choose Configure.
  5. Under Managed data identifiers, select the types of data to audit and mask. For example, choose EmailAddress, CreditCardNumber, and IpAddress.
  6. Choose Activate data protection.

Option 2: Create a data protection policy for a single log group

Use this option to target a specific API Gateway log group.

Complete the following steps:

  1. Open the Amazon CloudWatch console.
  2. In the navigation pane, under Logs, choose Log groups.
  3. Select the API Gateway execution log group (for example, API-Gateway-Execution-Logs_{api-id}/{stage-name}).
  4. Choose the Data protection tab.
  5. Choose Create policy.
  6. Under Managed data identifiers, select the types of data to audit and mask.
  7. Choose Activate data protection.

Note: When both an account-level and a log-group-level data protection policy exist, both policies apply to that log group. The service audits and masks all managed data identifiers that either policy specifies.

Add a custom data identifier for organisation-specific data

AWS managed data identifiers cover common sensitive data types, but they don't cover all organisation-specific patterns. For example, the x-api-key header value is not covered by managed identifiers.

To mask custom sensitive data, complete the following steps:

  1. Follow the steps in Option 1 or Option 2 up to the data identifier selection step.
  2. Choose Add custom data identifier.
  3. Enter a regex pattern that matches your sensitive data. For example, to match an API key value in the x-api-key header, use the following pattern.
(x-api-key[^a-zA-Z0-9]{1,10}[a-zA-Z0-9]{20,})
  1. Enter a descriptive name for the custom data identifier (for example, APIGateway-XApiKey).
  2. Complete the activation process.

Note: Adjust the regex pattern to match your specific API key format and how it appears in your log events.

Verify masking in log events

After you activate the data protection policy, confirm that sensitive data is masked in your log events.

Test 1: Before you apply the data protection policy (baseline)

With data tracing turned on and no data protection policy applied, the log events display the full request, including sensitive values in plaintext. The following example log event shows the x-api-key value in plaintext.

(059ad9a1-bcd3-4813-b251-1111111111) Endpoint request body after transformations:
{"resource":"/upload","path":"/upload","httpMethod":"POST","headers":{
"Accept":"/",
"User-Agent":"PostmanRuntime/7.51.1",
"x-api-key":"NLok19VsvB9zFJ6x4OprP7hQXg0uY3t999Er",
"X-Forwarded-For":"223.185.50.36",
"X-Forwarded-Port":"443",
"X-Forwarded-Proto":"https"} ... [TRUNCATED]

In this output, the x-api-key value NLok19VsvB9zFJ6x4OprP7hQXg0uY3t999Er is fully visible.

Test 2: After you apply the custom data identifier policy

After you apply the data protection policy with the custom data identifier for x-api-key, the same API request produces the following log event.

(5e98992c-928e-460b-9328-7d77dd5a326d) Endpoint request body after transformations:
{"resource":"/upload","path":"/upload","httpMethod":"POST","headers":{
"Accept":"/",
"User-Agent":"PostmanRuntime/7.51.1",
"***************************************",
"X-Forwarded-For":"223.185.50.36",
"X-Forwarded-Port":"443",
"X-Forwarded-Proto":"https"} ... [TRUNCATED]

In this output, the x-api-key header and its value are replaced with asterisks. All other request details remain visible for troubleshooting.

(Optional) Unmask protected data

To view the original unmasked data for debugging, the IAM user or role must have the logs:Unmask permission. Add the following IAM policy statement to the user or role that needs to view unmasked data.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "logs:Unmask",
      "Resource": "arn:aws:logs:REGION:ACCOUNT_ID:log-group:LOG_GROUP_NAME:"
    }
  ]
}

Note: Replace REGION, ACCOUNT_ID, and LOG_GROUP_NAME with your values.

After you grant this permission, open the CloudWatch Logs console, and then choose Show unmasked data from the Display dropdown to view the original values.

Important considerations

Take the following into account:

  • No data loss — CloudWatch Logs still stores the original unmasked data. Masking applies only at the display and output layer.
  • Audit capability — Configure data protection policies to send audit findings to an Amazon Simple Storage Service (Amazon S3) bucket, a CloudWatch Logs log group, or an Amazon Data Firehose delivery stream for compliance tracking.
  • Performance — Data protection policies add minimal latency to log ingestion and do not affect API Gateway performance.
  • Cost — CloudWatch Logs data protection incurs additional charges based on the volume of data scanned. For details, see CloudWatch pricing.
  • Retroactive masking — Data protection applies only to log events that the service ingests after you activate the policy. Existing log events are not retroactively masked.

Related information