Analyzing CloudWatch Metrics Service
1. Summarize CloudWatch Metrics API Calls
Aggregates all CloudWatch Metrics API calls by event name, ordered by frequency. Provides a high-level view of which APIs are being called most.
SELECT eventName AS API,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventSource = 'monitoring.amazonaws.com'
GROUP BY eventName
ORDER BY CallCount DESC
2. Summarize CloudWatch Metrics API Calls with Errors
Surfaces API calls that returned errors, grouped by event name, error code, and error message. Helps diagnose permission issues or throttling.
SELECT eventName AS API,
errorCode,
errorMessage,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE errorCode IS NOT NULL AND eventSource = 'monitoring.amazonaws.com'
GROUP BY eventName, errorCode, errorMessage
ORDER BY CallCount DESC
3. GetMetricData: Return IAM Role & Metric Insight Query
Shows the IAM role and Metric Insights expression for each GetMetricData call. Useful for understanding what queries are being executed against your metrics.
SELECT `@timestamp`,
eventName,
`userIdentity.sessionContext.sessionIssuer.arn` AS IAM_Role,
`requestParameters.metricDataQueries.0.expression` AS Metric_Insights_Query
FROM `LogGroup`
WHERE eventName = 'GetMetricData'
4. Summarize GetMetricData API Calls by Account ID
Identifies top callers of GetMetricData by AWS account. Useful in multi-account environments to pinpoint which account is generating the most read traffic.
SELECT `userIdentity.accountId` AS Account_ID,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'GetMetricData'
GROUP BY `userIdentity.accountId`
ORDER BY CallCount DESC
5. Summarize GetMetricData API Calls by IAM Role or User
Identifies top callers of GetMetricData by IAM identity. Reveals which roles or users are reading metrics most frequently.
SELECT `userIdentity.arn` AS IAM_Role_User,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'GetMetricData'
GROUP BY `userIdentity.arn`
ORDER BY CallCount DESC
6. Summarize GetMetricData API Calls by UserAgent & Client IP Address
Identifies top callers by user agent and source IP address. Helps determine whether calls originate from CLI, SDK, console, or third-party tools.
SELECT userAgent,
sourceIPAddress AS ClientIP,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'GetMetricData'
GROUP BY userAgent, sourceIPAddress
ORDER BY CallCount DESC
LIMIT 5
7. Summarize PutMetricData API Calls by Account ID
Identifies top publishers of custom metrics by AWS account. Useful for cost attribution and understanding which accounts contribute to metric volume.
SELECT `userIdentity.accountId` AS Account_ID,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'PutMetricData'
GROUP BY `userIdentity.accountId`
ORDER BY CallCount DESC
8. Summarize PutMetricData API Calls by IAM Role or User
Identifies top publishers of custom metrics by IAM identity. Reveals which roles or services are writing metrics most frequently.
SELECT `userIdentity.arn` AS IAM_Role_User,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'PutMetricData'
GROUP BY `userIdentity.arn`
ORDER BY CallCount DESC
9. Summarize PutMetricData API Calls by UserAgent & Client IP Address
Identifies publishers by user agent and source IP address. Helps determine whether metric data is coming from agents, SDKs, or services.
SELECT userAgent,
sourceIPAddress AS ClientIP,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'PutMetricData'
GROUP BY userAgent, sourceIPAddress
ORDER BY CallCount DESC
10. Summarize PutMetricData Calls by Entity, Resource Type, Client IP & Namespace
Detailed breakdown of PutMetricData by entity attributes, resource type, source IP, and namespace. Useful for identifying which resources and services are publishing custom metrics.
SELECT `requestParameters.entityMetricData.0.entity.keyAttributes.Identifier` AS Entity,
`requestParameters.entityMetricData.0.entity.keyAttributes.ResourceType` AS ResourceType,
sourceIPAddress AS ClientIP,
`requestParameters.namespace` AS Namespace,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'PutMetricData'
GROUP BY `requestParameters.entityMetricData.0.entity.keyAttributes.Identifier`,
`requestParameters.entityMetricData.0.entity.keyAttributes.ResourceType`,
sourceIPAddress,
`requestParameters.namespace`
ORDER BY CallCount DESC
11. Summarize PutMetricData Calls by UserAgent & Client IP Address
Identifies publishers by user agent and client IP address. A simplified view focused on the tool and network origin of metric publishing activity.
SELECT userAgent,
sourceIPAddress AS ClientIP,
COUNT(*) AS CallCount
FROM `LogGroup`
WHERE eventName = 'PutMetricData'
GROUP BY userAgent, sourceIPAddress
ORDER BY CallCount DESC
Usage Notes
- Replace
`LogGroup`with the name of your CloudTrail log group - Replace IAM ARNs and account IDs with values relevant to your environment
- These queries use the SQL query language — ensure your Log Insights console is set to SQL mode
- The CloudTrail event source for CloudWatch Metrics is
monitoring.amazonaws.com - Results can be visualized directly in a CloudWatch Dashboard using Log Insights widgets