Identify Long Duration Lambda Request

How to quickly identify the long duration request of your Lambda logs using Logs insights.

You can run below query that calculates average, maximum and minimum of all log event’s duration field by aggregating 5 minute period.

filter @type = “REPORT”
| stats avg(@duration), max(@duration), min(@duration) by bin(5m)

Thus, you will get to know what was avg, max and min of Duration field in the specified time window (lets say 30 minutes) and the interval of 5 minutes.
And you will get 6 entries in the log insight query result.

Or you may run below query that displays the requestId and duration of that request based on descending order. You can find top 10 highest duration request by using limit 10 command at the end.

filter @type = “REPORT”
| fields @requestId, @duration, @timestamp
| sort @duration desc
| limit 10

I have also attached additional documents for your reference which I believe will be useful for you in achieving your usecase.

Please let me know if you have further queries, I will be happy to assist.

Search for 504 error for requests that uses a specific API key

fields @timestamp, @message
| sort @timestamp desc
| filter status=”504″
| filter @message like /api-key-last-six-digits/
| limit 200

You can find more details for the API invocation from execution logs by searching with request id found with status 504 in your access logs using below filter.

fields @timestamp, @message, @logStream
| filter @message like /<request-id>/
| sort @timestamp asc
| limit 200

References :
[1] https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonCloudWatch%2Flatest%2Flogs%2FCWL_QuerySyntax.html&amp;data=05%7C01%7Cwei.fang%40esentire.com%7C13bd5673f1d5412e0b1808da3e018b4a%7Ceee603eaf9d24d258c8c4871f28f7767%7C1%7C0%7C637890473601005227%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=0tNwupWZWJQYVL0rmQfh4%2B%2BxDVJzbJ%2B65PwXfomcvyk%3D&amp;reserved=0
[2] https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Faws.amazon.com%2Fblogs%2Fcompute%2Foperating-lambda-using-cloudwatch-logs-insights%2F&amp;data=05%7C01%7Cwei.fang%40esentire.com%7C13bd5673f1d5412e0b1808da3e018b4a%7Ceee603eaf9d24d258c8c4871f28f7767%7C1%7C0%7C637890473601005227%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=KlUNjpPlSj%2BzDdz7IYTvPaHVrfce6OoBTzOqulwih7Q%3D&amp;reserved=0