Getting invocation count for Lambda functions

For one of our Software as a Service as a Service (SaaSaaS) clients, we built a serverless Lambda service and charged them per successful invocation.  Each time the service succeeds, we charge; therefore, we wanted to figure out how many times it was successful in order to calculate their bill.  Our service logs the text “Posting response” if the invocation was successful; if you want to get a total invocation count, in the steps below, replace “Posting response” with “END RequestId”.

 

  1. Follow these instructions to export your log data to an S3 bucket under your control: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/S3ExportTasksConsole.html
  2. Run this on your local commandline: aws s3 cp s3://bucket-with-the-export/exportedlogs lambda-exported-logs –recursive

  3. Run this on the local commandline to uncompress all the logs: gunzip -r lambda-exported-logs
  4. Install The Silver Searcher, a super-fast grep-like tool available on most platforms
  5. Fire up the ruby interpreter (type irb on your local commandline) and run this code:

    irb(main)> res=`ag -c "Posting response"`
    irb(main)> res.split(“\n”).map{|x|x.split(‘:’)[-1]}.map(&:to_i).inject(:+)

  6.  You’ll get your total invocation count.