AWS Lambda Python: Forcing the Lambda Sandbox to Timeout

import time
import datetime
runCount = 0def lambda_handler(event, context):
global runCount
current_dt = datetime.datetime.now()
print("Start of function: ", current_dt)
if runCount == 0:
current_dt = datetime.datetime.now()
print("I need to init: ", current_dt)
elif runCount < 3:
print("I don't need to init, just running: ", runCount)
else:
time.sleep(15)
runCount = runCount + 1current_dt = datetime.datetime.now()
print("end of function: ", current_dt)
return;
Start of function:  2021-12-17 17:07:20.445407
I need to init: 2021-12-17 17:07:20.445504
end of function: 2021-12-17 17:07:20.445517
Start of function:  2021-12-17 17:07:52.900683
I don't need to init, just running: 1
end of function: 2021-12-17 17:07:52.900751
...
Start of function: 2021-12-17 17:08:07.818484
I don't need to init, just running: 2
end of function: 2021-12-17 17:08:07.818551
"errorMessage": "2021-12-17T17:08:18.171Z 5c81669f-1fc2-45ca-b11d-0cdfe225ac72 Task timed out after 3.00 seconds"
Start of function:  2021-12-17 17:08:40.696788
I need to init: 2021-12-17 17:08:40.696861
end of function: 2021-12-17 17:08:40.696873
import time
import datetime
runCount = 0def lambda_handler(event, context):
global runCount
current_dt = datetime.datetime.now()
print("Start of function: ", current_dt)
if runCount == 0:
current_dt = datetime.datetime.now()
print("I need to init: ", current_dt)
elif runCount < 2:
print("I don't need to init, just running: ", runCount)
else:
raise Exception("Too many runs!")
runCount = runCount + 1current_dt = datetime.datetime.now()
print("end of function: ", current_dt)
return;
Start of function:  2021-12-17 17:13:31.610999
I need to init: 2021-12-17 17:13:31.611058
end of function: 2021-12-17 17:13:31.611070
...
Start of function: 2021-12-17 17:14:42.119543
I don't need to init, just running: 1
end of function: 2021-12-17 17:14:42.119606
Start of function:  2021-12-17 17:15:04.392925
[ERROR] Exception: Too many runs!
...
Start of function: 2021-12-17 17:15:24.882019
[ERROR] Exception: Too many runs!

--

--

--

Engineer, formerly at Amazon, currently at Google. All opinions are my own. Consider supporting here: https://devblabs.medium.com/membership

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Hack Days Work at Magoosh (and Why They’re Awesome!)

Across, Down, Diagonal — How We Test Crossword Puzzles on Android

Top 10 Dynamic programming problems

How to Avoid the Most Frequent UI Automation Pitfalls

Programming Again!!

Draw and Animate Bender’s Face with CSS

How To Fix Asus Zenfone 2 ZE551ML Not Charging [Troubleshooting Guide]

Bezier curves and time functions to smooth your movements

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brian Olson

Brian Olson

Engineer, formerly at Amazon, currently at Google. All opinions are my own. Consider supporting here: https://devblabs.medium.com/membership

More from Medium

Easiest Lambda Layers for Python Functions

Lambda UDFs for Amazon Redshift

AWS Glue As A Container

AWS KnowHow: How to Use Cross-Account IAM Roles with Lambda Functions — The Easy Way