- Newest
- Most votes
- Most comments
To identify unused routes in your HTTP API Gateway, you can use the following bash script. This script will iterate through all routes in your API and check their usage over a specified time period:
#!/bin/bash
# Variables
API_ID="YOUR_API_ID"
STAGE='YOUR_API_STAGE'
DAYS=1
# Get all routes in the HTTP API
echo "Getting all routes for HTTP API $API_ID..."
# Calculate time range
END_TIME=$(date +%s)
START_TIME=$((END_TIME - DAYS * 24 * 60 * 60))
# Convert to ISO format (works on both Linux and macOS)
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
START_TIME_ISO=$(date -u -r $START_TIME "+%Y-%m-%dT%H:%M:%SZ")
END_TIME_ISO=$(date -u -r $END_TIME "+%Y-%m-%dT%H:%M:%SZ")
else
# Linux
START_TIME_ISO=$(date -u --date="@$START_TIME" "+%Y-%m-%dT%H:%M:%SZ")
END_TIME_ISO=$(date -u --date="@$END_TIME" "+%Y-%m-%dT%H:%M:%SZ")
fi
# Process each route
aws apigatewayv2 get-routes --api-id $API_ID --query 'Items[*].[RouteKey]' --output text | while read -r ROUTE_KEY; do
# Skip empty route keys
if [ -z "$ROUTE_KEY" ]; then
echo "Skipping empty route key"
continue
fi
# Extract HTTP method from route key
HTTP_METHOD=$(echo $ROUTE_KEY | cut -d' ' -f1)
# Get metrics using correct dimensions
aws cloudwatch get-metric-statistics --namespace "AWS/ApiGateway" --metric-name "Count" --dimensions \
Name=ApiId,Value="$API_ID" \
Name=Resource,Value="/" \
Name=Stage,Value="$STAGE" \
Name=Method,Value="$HTTP_METHOD" \
--start-time "$START_TIME_ISO" --end-time "$END_TIME_ISO" --period 60 --statistics Sum --output json > metric_result.txt 2>metric_error.txt
# Check if there was an error
if [ -s metric_error.txt ]; then
echo "Error getting metrics for route: $ROUTE_KEY"
cat metric_error.txt
else
# Process the result
COUNT=$(cat metric_result.txt | jq -r '.Datapoints[].Sum // "0"' | awk '{sum += $1} END {print sum}')
if [ -z "$COUNT" ] || [ "$COUNT" = "0" ]; then
echo "UNUSED ROUTE: $ROUTE_KEY"
else
echo "Route in use: $ROUTE_KEY - $COUNT requests in last $DAYS days"
fi
fi
# Small delay to avoid API throttling
sleep 1
done
# Clean up
rm -f metric_result.txt metric_error.txt
echo "Scan complete!"
To use this script:
Replace YOUR_API_ID with your actual API Gateway ID. Replace YOUR_API_STAGE with your API stage name. Adjust the DAYS variable to set the time range for checking usage (default is 1 day).
This script does the following:
- It retrieves all routes from your API Gateway.
- For each route, it queries CloudWatch metrics for the "Count" metric, which represents the number of API calls.
- It checks the usage for each route over the specified time period.
- It outputs whether each route is used or unused, along with the request count for used routes.
Some important points to note:
- The script uses the AWS CLI, so make sure it's installed and configured with the necessary permissions.
- It requires jq for JSON processing, so install it if not already present.
- The script includes a small delay between API calls to avoid throttling.
- It handles both Linux and macOS date formats.
To address your specific situation where you only see usage data for 340 out of 500+ routes: Run this script to get a comprehensive list of all routes and their usage. The script will explicitly list unused routes, helping you identify which of your 500+ routes are not being accessed. For routes showing as unused, double-check if they are correctly configured and accessible. Consider setting the DAYS variable to a larger number (e.g., 30 or 90) to account for less frequently used routes. After running the script, you'll have a clear picture of which routes are unused. You can then decide whether to keep these routes, update them, or remove them from your API Gateway configuration.
Remember that some routes might be used very infrequently, so consider your use case when deciding what constitutes an "unused" route. You may want to run this script periodically or for different time ranges to get a more comprehensive view of route usage over time.
Important Note: This script has been validated with limited data and worked as expected in those test cases. It is provided as guidance and a starting point for identifying unused routes, but may not fully solve your specific use case. You may need to modify and adapt it based on your particular API Gateway configuration, usage patterns, and requirements. Always test thoroughly in a non-production environment first.
answered a year ago
Relevant content
asked 6 years ago
- AWS OFFICIALUpdated 9 months ago
