16

How do I get the usage metric from AWS API Gateway by API key, e.g. usage counter, usage logs, etc.?

I am currently sending the x-api-key to my integration endpoint for manual logging. I would prefer not to do this and let AWS API Gateway measure and report this metric for me.

Tri Q Tran
  • 5,500
  • 2
  • 37
  • 58

2 Answers2

16

I found that all the logging was insufficient for what I needed - especially as it didn't log per API Key as yet (holding out that this will still one).

So I created my own custom logs -

This way, I can search my CloudWatch logs and get back the exact data that I'm wanting, even per API Key if required;

Under my stages, I enabled the "Custom Access Logging" and used the following format:

enter image description here

NOTE: These custom logs, currently only support context variables.

  • I'm waiting for the support of input variables:

Documentation can be found here:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

With this custom logging in place, I can open CloudWatch, filter per date and use a search string to search for anything I want (that fits my custom logging);

  • For example: All GET requests made for any API Key ending in BcxvY1 on Endpoint /fees

[RequestId,APIKeyText,APIKeyValue="*BcxvY1*",HTTPText,MethodText,HTTPMethodType="*GET*",PathText,PathValue="*/fees,",StatusText,StatusCode,ErrorsText,ErrorsValue,DescriptionText,DescriptionValue=custom_log]

The great thing with this, is that is completely customisable. I can change my search query however I want, depending on the results I want. Making it more / less complex as needed.

Hope this helps.

Hexie
  • 3,955
  • 6
  • 32
  • 55
15

So far, there are no metrics in Cloudwatch for key usage. But the gateway itself keeps some usage statistics, although not very detailed.

Usage plan overview: usage plan stats

Invocation statistic of one API Key: api key usage

jens walter
  • 13,269
  • 2
  • 56
  • 54
  • 1
    does anyone know how to programmatically query this information? I'd love to report this data to Stripe billing API for metered billing – AxelTheGerman Jul 20 '18 at 07:03
  • 5
    This data can be retrieved through a 'get-usage' call, https://docs.aws.amazon.com/cli/latest/reference/apigateway/get-usage.html – jens walter Jul 20 '18 at 08:41