Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add AWS cloudwatch log metrics filter to graph memory usage by lambda
#! /usr/bin/env sh
##
# Notes:
# Log line:
# REPORT RequestId: f420d819-d07e-11e8-9ef2-4d8f649fd167 Duration: 158.15 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 38 MB
# [ report_label="REPORT", ..., label="Used:", max_memory_used_value, unit="MB" ]
#
# Test the metric filter
# aws logs test-metric-filter \
# --filter-pattern '[ report_label="REPORT", ..., label="Used:", max_memory_used_value, unit="MB" ]' \
# --log-event-messages "REPORT RequestId: f420d819-d07e-11e8-9ef2-4d8f649fd167 Duration: 158.15 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 38 MB"
##
# get list of log groups
# filter by those named /aws/lambda/
prefix="/aws/lambda/"
log_groups=$(aws logs describe-log-groups --log-group-name-prefix $prefix | jq -r '.logGroups[].logGroupName')
# for each log group
for log_group in $log_groups; do
fn_name=${log_group#$prefix};
echo "adding metric filter for $fn_name"
aws logs put-metric-filter \
--log-group-name "$log_group" \
--filter-name sandfox-exec-report \
--filter-pattern '[ report_label="REPORT", ..., label="Used:", max_memory_used_value, unit="MB" ]' \
--metric-transformations "metricName=${fn_name}-maxMemory,metricNamespace=LogMetrics/Sandfox/Lambda,metricValue=\$max_memory_used_value"
done
echo "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.