This is the companion repo for the blog post: Bespoke CloudFront Edge Metrics The stack will create a CloudFront distribution that uses a CloudFront function to emit latency metrics and queryable logs for a Lambda function as measured by the CloudFront edge.
lib/bespoke-edge-metrics-stack.mjs
- The CDK stack that creates the CloudFront distribution, CloudFront functions, Lambda Function/Function Url.cloudfront-functions/metricsAspect.js
The CloudFront function that injects thex-request-time
header and processes all of thex-meta-*
response headers from the lambda function to emit a latency metric in the EMF format to the log.src/handler.js
- The Lambda function that sets response headers so they are accessible in the CloudFront Function.
npm install
If you've never used the CDK before, run:
npx cdk bootstrap
npx cdk deploy
Outputs will print the url for the CloudFront distribution.
BespokeEdgeMetricsStack.CloudFrontUrl = https://d1xxxxxxxxxxxx.cloudfront.net/api/hello
Hit the url a few times, then if you are logged into the AWS console, use these links to see your metrics and logs. There may be an initial delay of 2-3 minutes before your metrics and logs are available.
You can see the metrics in CloudWatch Metrics here
You can see the logs in CloudWatch Logs here
npx cdk destroy
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpx cdk deploy
deploy this stack to your default AWS account/regionnpx cdk diff
compare deployed stack with current statenpx cdk synth
emits the synthesized CloudFormation template