Monitor application activity by using CloudWatch Logs Insights
Ram Kandaswamy, Amazon Web Services
Summary
This pattern provides a solution for automatically detecting and alerting on application exceptions by using Amazon CloudWatch Logs Insights. By implementing automated log analysis and alerting, you can quickly identify and respond to application issues in your production environment.
Logs play an important role in monitoring system behavior, identifying issues, and supporting application performance. During a migration, log files help you validate how the system functions in the new environment, detect compatibility problems, and identify unexpected behaviors. Issues can relate to operations or security. For security-related issues, early detection of unauthorized access attempts or suspicious activities helps you maintain security and meet regulatory requirements. This capability is especially important when you work with sensitive data or critical systems.
This pattern is particularly valuable for teams that need to do the following:
Maintain high application availability.
Respond to production issues quickly.
Analyze application-specific errors not captured by AWS service logs.
Perform on-demand log analysis without pre-built infrastructure.
CloudWatch Logs Insights is well suited for analyzing application-generated logs where the error context exists only within your application code. CloudWatch Logs Insights helps you perform the following tasks:
Query unstructured or semi-structured log data.
Perform on-demand analysis during incident response.
Correlate events across multiple log groups.
Create quick visualizations without external tools.
Prerequisites and limitations
Prerequisites
An active AWS account with a production application deployed
Basic understanding of the production application's logging format and exception patterns
Application logs configured to stream to Amazon CloudWatch Logs
Limitations
Some AWS services aren’t available in all AWS Regions. For Region availability, see AWS Services by Region
. For specific endpoints, see Service endpoints and quotas , and choose the link for the service.
Architecture
The following diagram shows how CloudWatch Logs Insights evaluates resource logs and sends a relevant data visualization to a CloudWatch dashboard.

The diagram shows the following workflow:
The resources publish logs to CloudWatch Logs. Resources can include AWS resources such as Amazon Elastic Compute Cloud (Amazon EC2) instances or Amazon Simple Storage Service (Amazon S3) buckets. On-premises systems that have the CloudWatch agent installed can also publish logs to CloudWatch.
CloudWatch Logs Insights filters for the relevant pattern string. Examples of search pattern strings include "error", "exception", or a specific regular expression.
Typically, the production support team or developers add the pattern visualization to the CloudWatch dashboard.
Automation and scale
Developers can automate this pattern’s solution by using the AWS Cloud Development Kit (AWS CDK), AWS CloudFormation, or AWS SDKs to handle multiple string patterns. Teams can incorporate this automation into their continuous integration and continuous delivery (CI/CD) processes.
Tools
AWS services
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. AWS Identity and Access Management (IAM)
helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them. AWS Key Management Service (AWS KMS)
helps you create and control cryptographic keys to help protect your data.
Best practices
Query efficiency
Define and configure log groups
to analyze relevant log data. Use field explorers to understand the structure and fields available in your log data.
Write efficient queries by using CloudWatch Logs Insights query syntax
. Adapt sample queries
to your specific requirements for quicker analysis. Limit query time ranges to reduce data scanned and improve performance.
Save queries
for future use to save time and ensure consistent analysis.
Security
Apply IAM policies
to CloudWatch Logs Insights and log groups. Follow the principle of least privilege and grant only the permissions required to perform a task. For more information, see Grant least privilege and Security best practices in the IAM documentation. Enable log data encryption using AWS KMS
for sensitive log data.
Cost optimization
CloudWatch Logs Insights charges per GB of data scanned per query. Narrow time ranges and target specific log groups to reduce costs.
Configure log retention policies that match your compliance and business requirements to manage storage costs.
For frequent analysis of large historical datasets, consider exporting logs to Amazon S3 and using Amazon Athena.
Review CloudWatch pricing
to understand cost implications for your use case.
Epics
| Task | Description | Skills required |
|---|---|---|
Configure IAM permissions. | To configure IAM permissions, do the following:
Follow the principle of least privilege, and limit each permission to the specific log groups, dashboards, and alarms that the role needs. For the resource types that each action supports, see Actions, resources, and condition keys for Amazon CloudWatch Logs and Actions, resources, and condition keys for Amazon CloudWatch in the Service Authorization Reference. For information about how to create IAM policies or to add permissions to existing policies, see Define custom IAM permissions with customer managed policies | AWS administrator, AWS DevOps, AWS systems administrator, Cloud administrator, Cloud architect, DevOps engineer |
Create a log group. | To create a log group, use any of the following options:
| AWS administrator, AWS DevOps, AWS systems administrator, Cloud administrator, Cloud architect, DevOps engineer |
Generate a CloudWatch Logs Insights query. | To create and save a CloudWatch Logs Insights query, do the following:
| AWS administrator, AWS DevOps, AWS systems administrator, Cloud administrator, Cloud architect, DevOps engineer |
Create visualization in a CloudWatch dashboard. | To use a CloudWatch dashboard to create a visualization, do the following:
For more information about dashboard options and capabilities, see Using Amazon CloudWatch dashboards | AWS administrator, AWS DevOps, AWS systems administrator, Cloud administrator, Cloud architect, DevOps engineer |
Troubleshooting
| Issue | Solution |
|---|---|
Unable to see query results or query seems broken | Start with a working query that was modified from a sample query |
Log groups not creating log streams | Confirm that the application's IAM role has the logs:CreateLogStream and logs:PutLogEvents permissions for the log streams in the log group. IAM evaluates these actions against the log stream ARN, not the log group ARN. |
Query timeout or slow performance | Reduce the time range, target specific log groups, or simplify the query. Complex regular expression ( |
No data returned for valid time range | Verify log group selection and check that logs are being ingested (review log streams), and confirm the filter pattern matches your log format. |