Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ankur0101/65caa89cedc34bc4e3b5d4ecfde405a6 to your computer and use it in GitHub Desktop.
Save ankur0101/65caa89cedc34bc4e3b5d4ecfde405a6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Replace with your actual API Gateway ID
API_GATEWAY_ID="your-id-here"
# Header for CSV format
echo "Resource ID,Resource Path,HTTP Method,Integration,Authorizer Name,Authorizer Type"
# Step 1: Get a list of resources and paths
resources_info=$(aws apigateway get-resources --rest-api-id "$API_GATEWAY_ID" --query 'items[].{id: id, path: path}' --output json)
# Step 2: Loop through each resource and get the integration and authorizer details
for row in $(echo "$resources_info" | jq -r '.[] | @base64'); do
_jq() {
echo "$row" | base64 --decode | jq -r "$@"
}
resource_id=$(_jq '.id')
resource_path=$(_jq '.path')
# Replace with your desired HTTP methods (e.g., GET, POST, etc.)
http_methods=("GET" "POST")
for http_method in "${http_methods[@]}"; do
integration=$(aws apigateway get-integration --rest-api-id "$API_GATEWAY_ID" --resource-id "$resource_id" --http-method "$http_method" --query 'uri' --output text 2>&1)
if [ $? -eq 0 ]; then
# Check if integration is with Lambda (assuming it includes "lambda" in the URI)
if [[ $integration == *"lambda"* ]]; then
# Fetch the authorizer ID for the current resource and method
authorizer_id=$(aws apigateway get-method --rest-api-id "$API_GATEWAY_ID" --resource-id "$resource_id" --http-method "$http_method" --query 'authorizerId' --output text)
if [ "$authorizer_id" != "None" ]; then
# Fetch authorizer details if an authorizer is attached
authorizer_info=$(aws apigateway get-authorizer --rest-api-id "$API_GATEWAY_ID" --authorizer-id "$authorizer_id" 2>&1)
if [ $? -eq 0 ]; then
authorizer_name=$(echo "$authorizer_info" | jq -r '.name')
authorizer_type=$(echo "$authorizer_info" | jq -r '.type')
echo "$resource_id,$resource_path,$http_method,$integration,$authorizer_name,$authorizer_type"
else
# Print the error message when there is an issue fetching the authorizer
echo "$resource_id,$resource_path,$http_method,$integration,-,Error fetching authorizer details"
fi
else
echo "$resource_id,$resource_path,$http_method,$integration,-,-"
fi
fi
else
# Print the error message when the integration is not found
echo "$resource_id,$resource_path,$http_method,Error fetching integration,-,-"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment