Search code examples
amazon-web-servicesamazon-cloudfrontamazon-cloudwatch

How to have a CloudWatch metric based on CloudFront URL pattern requests count?


I have a CloudFront distribution and need to get a number of requests for some URL pattern (e. g. /user/[0-9]+/profile.html). I would like to have it as a ClowdWatch metric because I want to combine it with some other metrics and set an alert.

What is the easiest way to do it?

I see CloudFront sends some predefined metrics to CloudWatch including number of requests, but I can't filter it with an URL pattern. Also, I see CloudFront sends logs to S3, is it possible to have the same logs in CloudWatch so I can filter by the pattern I need?


Solution

  • CloudFront predefined "Requests" metrics shows total requests and CloudFront can only send logs to S3 bucket, it doesn't sent it to CloudWatch log group. In my opinion, the best method is to use Lambda. You can enable Event notification on S3 to trigger lambda on PUT request, lambda function can parse the file and count the events for URI /user/[0-9]+/profile.html and publish data points directly on CloudWatch metric. Code is the tricky part here.