2

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
  • try passing --no-paginate to get-usage? Commented Jan 25, 2021 at 7:13

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi. Just wonder if you could get same with just using --query of aws cli, without the need for jq or xargs?
Not sure about replacing the xargs, but you can remove the need for jq: aws apigateway get-usage-plan-keys --usage-plan-id xxxxxx --query "items[*].id" --output text | xargs -n1 aws apigateway get-usage --usage-plan-id xxxxxx --start-date YYYY-MM-DD --end-date YYYY-MM-DD --no-paginate --key-id > output.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.