AWS CLI: api gateway get-usage not returning usage of all keys

Viewed 335

I'm trying to see usage of each API key in my usage plan. However when I run

aws apigateway get-usage --usage-plan-id ***** --start-date 2021-01-18 --end-date 2021-01-24 --profile prod --region ap-south-1 >> week04.txt 

It returns usage of only few keys. When I try to do that from the AWS management console from here

Snapshot

It gives me the usage of the same few keys. I have to manually click every single key and generate the report which is very tedious. What am I doing wrong? Please help.

1 Answers

We can combine get-usage-plan-keys and get-usage with xargs to extract for every api key at once.

aws apigateway  get-usage-plan-keys --usage-plan-id xx44ww | jq -r ".items[].id" | xargs -I {} aws apigateway get-usage --usage-plan-id xg4j0w --key-id {} --start-date 2021-01-01 --end-date 2021-01-24 --no-paginate > output.json

Breaking it down for better readability:

Getting usage plan keys:

aws apigateway  get-usage-plan-keys --usage-plan-id xx44ww

Extracting plan keys

| jq -r ".items[].id"

loop for every api key id with xargs

aws apigateway get-usage --usage-plan-id xg4j0w --key-id {} --start-date 2021-01-01 --end-date 2021-01-24 --no-paginate
Related