0

I am looking for an (boto3) API of aws lambda or aws cloud watch, which can tell me the max memory actually used by a lambda function on execution?

I know on every execution the lambda function prints the result which has 'max memory used' but I need an API for it

7wick
  • 411
  • 4
  • 17

2 Answers2

1

We have no API to get that information directly. But you can get MemoryUsedInMB from Cloudwatch metrics.

At Cloudwatch metrics, find All > Lambda > By Function Name then you can see used memory in Invocations

Cloudwatch Lambda Metric => https://docs.aws.amazon.com/lambda/latest/dg//monitoring-functions-metrics.html

How to get metrics data => https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.get_metric_data

Tuan Vo
  • 1,875
  • 10
  • 10
  • So the namespace will be ' Invocations' and metric name will be 'MemoryUsedInMB' ? Also what will be the dimensions? @TuanVA – 7wick Jan 17 '20 at 06:41
  • Metric name is invocations, dimensions is all columns in metric – Tuan Vo Jan 17 '20 at 07:06
  • @TuanVo idk how are you saying that invocation metric will provide me MemoryUsedInMb. Also, i tried the API. This is the sample response - {'MetricDataResults':[{'Id':'sdfnklwedsd','Label':'Invocations','Timestamps':[datetime.datetime(2021,10,13,7,5,tzinfo=tzlocal()),datetime.datetime(2021,10,13,7,0,tzinfo=tzlocal())],'Values':[1.0,1.0],'StatusCode':'Complete'}],'Messages':[]} – bhavuk bhardwaj Oct 13 '21 at 07:13
0

You can get that info using get_log_events e.g.

import boto3
logs_client = boto3.client('logs', region_name='us-east-2')
logGroupName='/aws/lambda/My_Function',
logStreamName='2022/07/09/[$LATEST]3aevb813776865536a146697b7379d5',

response = logs_client.get_log_events(
    logGroupName=logGroupName,
    logStreamName=logStreamName,
    limit=123,
)

response will have all the memory and duration info that you normally see in the console.

I hardcoded logGroupName and logStreamName but you can also just get them using describe_log_streams Example below only gets the latest log stream.

stream_response = logs_client.describe_log_streams(
            logGroupName = "/aws/lambda/My_Function", 
            orderBy='LastEventTime',                 
            descending=True,
            limit=1                                  
            )
print(stream_response)
West
  • 2,350
  • 5
  • 31
  • 67