Monitoring Lambda memory usage - X-Ray aware version

Tuesday, 30 July 2019, 07:17

In serverless architecture on AWS Lambda is a basic computing service. AWS provides some basic metrics related to it, like execution time, invocations count etc. but one important metric is missing - memory consumption. Knowing how much memory your functions use allows you to optimize resources and reduce costs (or increase to avoid failures). It can be very simply generated out of logs (since Lambda logs execution report after each invocation) with CloudWatch metric filter feature. And this approach has been described plenty of times around the web, including ready solutions. So I will not elaborate that much on the concept, as has already been done. But there is one trick - if you use X-Ray this solutions will not work. At least - not out of the box.

Problem is, that if you use AWS X-Ray to trace your Lambda invocations, it appends additional tracing information to the final report. Not a big deal, but the patterns stop to work. If you encounter use mentioned approach to monitor your functions, enabled tracing and encounter this problem, here is the updated pattern that includes X-Ray tracing components:

        Type: "AWS::Logs::MetricFilter"
            LogGroupName: !Ref "AuthorizerLambdaLogGroup"
            FilterPattern: "[label=\"REPORT\", ..., memory_label=\"Used:\", max_memory_used, unit=\"MB\", xray_label=\"XRAY\", trace_label=\"TraceId:\", traced, segment_label=\"SegmentId:\", segment]"
                    MetricValue: "$max_memory_used"
                    MetricNamespace: "WrzasqPl/Lambda"
                    MetricName: !Sub "${AuthorizerLambda}-Memory"

Keep in mind however, that in Lambda memory size pairs with CPU power - you may want to to extend your computing power which will also increase associated memory, but it will just be a side-effect.

Tags: , , , , , ,