I want to know bytes transferred to the user by CloudFront in the response body. I want this for each individual user viewer response, not just an aggregated metric of overall CloudFront usage. Also, I’d like to get it immediately and programmatically, not in a delayed fashion, say, from a CloudWatch log. My goal is to bill each individual user a data transfer charge that’s based on the data transfer charges that I pay, assuming that I’ve passed the 1TB per month free CloudFront tier, and I want to show these charges to them immediately so they can see them accumulate on a request-by-request basis.
I thought of attaching a Lambda@Edge function to the distribution’s “Viewer Response” event, but I don’t see how to consistently get the response body size from inside that function. Response headers are available in there, but it seems some responses have a Content-Length header and some don’t. I don’t see any way to access the actual response body in that function.
Trying to track the bytes transferred from each origin CloudFront is serving doesn’t seem right, because I’m not charged for that, and, if CloudFront is caching then bytes sent from the origin would be way too low, anyway.
It seems there must be some way to recover the bytes that CloudFront is sending on a response-by-response basis.