Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway usage by API key

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.

like image 702
Tri Q Tran Avatar asked May 22 '17 07:05

Tri Q Tran


2 Answers

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

like image 154
jens walter Avatar answered Sep 19 '22 19:09

jens walter


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.

like image 41
Hexie Avatar answered Sep 22 '22 19:09

Hexie