Secure sensitive data in CloudWatch Logs by using Amazon Macie
Anisha Salunkhe, Omar Franco, and David Guardiola, Amazon Web Services
Summary
This pattern shows you how to use Amazon Macie to automatically detect sensitive data in an Amazon CloudWatch Logs log group by implementing a comprehensive security monitoring workflow. The solution uses Amazon Data Firehose to stream CloudWatch Logs entries to Amazon Simple Storage Service (Amazon S3). Macie periodically scans this bucket for personally identifiable information (PII), financial data, and other sensitive content. The infrastructure is deployed through a AWS CloudFormation template that provisions all necessary AWS services and configurations.
CloudWatch Logs often contains application data that can inadvertently include sensitive user information. This can create compliance and security risks. Traditional log monitoring approaches lack automated sensitive data detection capabilities. This can make it difficult to identify and respond to potential data exposures in real-time.
This pattern helps security teams and compliance officers maintain data confidentiality by providing automated detection and alerting for sensitive data in logging systems. This solution enables proactive incident response through Amazon Simple Notification Service (Amazon SNS) notifications, and it automatically isolates sensitive data to a secure Amazon S3 bucket. You can customize the detection patterns and integrate the workflow with your existing security operations processes.
Prerequisites and limitations
Prerequisites
An active AWS account
Permissions to create a CloudFormation stack
A CloudWatch Logs log group that you want to monitor
An active email address to receive notifications from Amazon SNS
Access to AWS CloudShell
(Optional) Access to the AWS Command Line Interface (AWS CLI), installed and configured
Limitations
Macie is subject to service quotas. For more information, see Quotas for Macie in the Macie documentation.
Architecture
Target architecture
The following diagram shows the workflow for using Macie to examine CloudWatch Logs log entries for sensitive data.

The workflow shows the following steps:
The CloudWatch Logs log group generates the logs, which are subject to the subscription filter.
The subscription filter forwards the logs to Amazon Data Firehose.
The logs are encrypted with an AWS Key Management Service (AWS KMS) key when they pass through the Amazon Data Firehose delivery stream.
The delivery stream delivers the logs to the exported logs bucket in Amazon S3.
At 4 AM each day, Amazon EventBridge initiates an AWS Lambda function that starts a Macie scan for sensitive data in the exported logs bucket.
If Macie identifies sensitive data in the bucket, a Lambda function removes the log from the exported logs bucket and encrypts it with an AWS KMS key.
The Lambda function isolates the logs that contain sensitive data in the data isolation bucket.
The identification of sensitive data initiates an Amazon SNS topic.
Amazon SNS sends an email notification to an email address that you configure with information about the logs that contain sensitive data.
Deployed resources
The CloudFormation template deploys the following resources in your target AWS account and AWS Region:
Two Amazon S3 buckets:
An exported logs bucket for storing the CloudWatch Logs data
A data isolation bucket to store the sensitive information
An Amazon EventBridge rule that responds to Macie findings
AWS Lambda functions that initiate events and export logs to Amazon S3 buckets
An Amazon SNS topic and subscription
An Amazon Data Firehose stream
A Macie session
A Macie custom data identifier
A CloudWatch Logs subscription filter
AWS KMS keys to encrypt the logs stored in the buckets
The necessary AWS Identity and Access Management (IAM) roles and policies for the solution
Tools
AWS services
AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and AWS Regions.
Amazon CloudWatch Logs helps you centralize the logs from all your systems, applications, and AWS services so you can monitor them and archive them securely.
Amazon Data Firehose helps you deliver real-time streaming data to other AWS services, custom HTTP endpoints, and HTTP endpoints owned by supported third-party service providers.
Amazon EventBridge is a serverless event bus service that helps you connect your applications with real-time data from a variety of sources. For example, sources such as AWS Lambda functions, HTTP invocation endpoints using API destinations, or event buses in other AWS accounts.
AWS Key Management Service (AWS KMS) helps you create and control cryptographic keys to help protect your data.
AWS Lambda is a compute service that helps you run code without needing to provision or manage servers. It runs your code only when needed and scales automatically, so you pay only for the compute time that you use.
Amazon Macie helps you discover sensitive data, provides visibility into data security risks, and enables automated protection against those risks.
Amazon Simple Notification Service (Amazon SNS) helps you coordinate and manage the exchange of messages between publishers and clients, including web servers and email addresses.
Amazon Simple Storage Service (Amazon S3) is a cloud-based object storage service that helps you store, protect, and retrieve any amount of data.
Code repository
The code for this pattern is available in the GitHub sample-macie-for-securing-cloudwatch-logs
Best practices
Follow the CloudFormation best practices in the CloudFormation documentation.
Epics
| Task | Description | Skills required |
|---|---|---|
Clone the code repository. | Enter the following command to clone the repository to your local workstation:
| App developer |
(Optional) Edit the CloudFormation template. |
| App developer |
Option 1 – Deploy using script with command-line parameters. | Enter the following command to deploy the solution by using command line parameters, where the value for
| General AWS |
Option 2 – Deploy using script with environment variables. |
| General AWS |
Option 3 – Deploy using the AWS CLI. | Enter the following command to deploy the solution by using the AWS CLI, where the value for
| |
Option 4 – Deploy through the AWS Management Console. |
| General AWS |
Monitor the deployment status and confirm deployment. |
| General AWS |
Confirm the Amazon SNS subscription. | Follow the instructions in Confirm your Amazon SNS subscription in the Amazon SNS documentation to confirm your Amazon SNS subscription. | App developer |
| Task | Description | Skills required |
|---|---|---|
Option 1 – Test with automated reporting. | If you used the default stack name, enter the following command to test the solution:
If you used a custom stack name, enter the following command to test the solution:
If you used a custom stack name and custom parameters, enter the following command to test the solution:
| General AWS |
Option 2 – Test with targeted validation. |
| General AWS |
| Task | Description | Skills required |
|---|---|---|
Option 1 – Perform automated cleanup. | If you used the default stack name, enter the following command to delete the stack:
If you used a custom stack name, enter the following command to delete the stack:
If you used a custom stack name and custom parameters, enter the following command to delete the stack:
| General AWS |
Option 2 – Perform step-by-step cleanup. |
| General AWS |
Verify clean up. |
| General AWS |
Troubleshooting
| Issue | Solution |
|---|---|
CloudFormation stack status shows CREATE_FAILED. | The CloudFormation template is configured to publish logs to CloudWatch Logs. You can view the logs in the AWS Management Console so that you don't have to connect to your Amazon EC2 instance. For more information, see View CloudFormation logs in the console |
CloudFormation | Some resources must be empty before they can be deleted. For example, you must delete all objects in an Amazon S3 bucket or remove all instances in an Amazon EC2 security group before you can delete the bucket or security group. For more information, see Delete stack fails in the Amazon S3 documentation. |
Error when parsing a parameter. | When you use the AWS CLI or the CloudFormation console to pass in a value, add the quotation marks. |
Related resources
Architecture best practices for storage
(AWS website) Filter pattern syntax for metric filters, subscription filters, filter log events, and Live Tail (CloudWatch Logs documentation)
Designing and implementing logging and monitoring with Amazon CloudWatch (AWS Prescriptive Guidance)
Troubleshooting CloudFormation (CloudFormation documentation)