Hi all,
I’m currently using the following method to retrieve AWS HTTP/RESTful API call counts:
import boto3
from datetime import datetime
cloudwatch = boto3.client('cloudwatch')
apigateway = boto3.client('apigateway') # REST API
apigatewayv2 = boto3.client('apigatewayv2') # HTTP API
start_time = datetime(2025, 3, 6)
end_time = datetime(2025, 4, 6)
period = 604800 # 7 天(秒)
results = []
rest_apis = apigateway.get_rest_apis()['items']
for api in rest_apis:
api_name = api['name']
api_id = api['id']
response = cloudwatch.get_metric_statistics(
Namespace='AWS/ApiGateway',
MetricName='Count',
StartTime=start_time,
EndTime=end_time,
Period=period,
Statistics=['Sum'],
Dimensions=[{'Name': 'ApiName', 'Value': api_name}]
)
datapoints = response['Datapoints']
total_count = sum(point['Sum'] for point in datapoints) if datapoints else 0
results.append({'Type': 'REST API', 'ApiName': api_name, 'ApiId': api_id, 'TotalCount': total_count})
http_apis = apigatewayv2.get_apis()['Items']
for api in http_apis:
api_name = api['Name']
api_id = api['ApiId']
response = cloudwatch.get_metric_statistics(
Namespace='AWS/ApiGateway',
MetricName='Count',
StartTime=start_time,
EndTime=end_time,
Period=period,
Statistics=['Sum'],
Dimensions=[{'Name': 'ApiId', 'Value': api_id}] #
)
datapoints = response['Datapoints']
total_count = sum(point['Sum'] for point in datapoints) if datapoints else 0
results.append({'Type': 'HTTP API', 'ApiName': api_name, 'ApiId': api_id, 'TotalCount': total_count})
results_sorted = sorted(results, key=lambda x: x['TotalCount'], reverse=True)
print("-" * 70)
for result in results_sorted:
print(f" {result['Type']}, : {result['ApiName']}, API ID: {result['ApiId']}, : {result['TotalCount']}")
While most of the API calls are successfully captured, I’ve noticed that some calls appear to be missing from the logs.
Could this be due to permission issues or any other reason that might be preventing certain API calls from being tracked?
Would appreciate any insights or suggestions. Thank you!