Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Created December 7, 2023 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuelnk/682c653452143318d989b3445a856315 to your computer and use it in GitHub Desktop.
Save emmanuelnk/682c653452143318d989b3445a856315 to your computer and use it in GitHub Desktop.
AWS Cloudwatch Log Insights Useful Lambda Queries

Queries

General Performance

This query runs against Lambda function logs to get the following information:

  • InvocationCount -- This is the number of times the Lambda function was invoked.
  • ColdStartCount -- This is the number of times the Lambda function had initialization overhead (cold start).
  • InitDuration (min, avg, max) -- This is the amount of time it took the Lambda function to initialize (cold start time).
  • Duration (min, avg, max) -- This is the amount of time it took the Lambda function to run after initialization.
  • AllocatedMemory -- This is the amount of memory allocated to the Lambda function in MB.
  • MaxMemoryUsed (min, avg, max) -- This is the max amount of memory that the Lambda function used during its invocation in MB.

Note: @ignored* is placeholder field that is necessary due to the way Cloudwatch Insights parses text fields for information

filter @type = "REPORT"
| parse @log "*/aws/lambda/*" as @ignored1, @functionName
| parse @message "*Memory Size: * MB*Init Duration: * ms*" as @ignored2, memorySize, @ignored3, @initDuration
| stats count(@message) as @invocationCount,
        count(@initDuration) as @coldStartCount,
        min(@initDuration)/1000 as @minInitDuration, 
        avg(@initDuration)/1000 as @avgInitDuration, 
        max(@initDuration)/1000 as @maxInitDuration,
        min(@duration)/1000 as @minDuration, 
        avg(@duration)/1000 as @avgDuration, 
        max(@duration)/1000 as @maxDuration,
        max(memorySize) as allocatedMemory,
        min(@maxMemoryUsed)/(1024*1024) as @minMaxMemoryUsed, 
        avg(@maxMemoryUsed)/(1024*1024)  as @avgMaxMemoryUsed, 
        max(@maxMemoryUsed)/(1024*1024) as @maxMaxMemoryUsed
        by @functionName
| sort by @invocationCount desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment