How to fix Python 3.12 datetime.utcnow() deprecation warnings in AWS Lambda
Upgrading AWS Lambda functions to the Python 3.12 runtime triggers DeprecationWarnings for datetime.datetime.utcnow() and utcfromtimestamp(), which Python now flags as scheduled for removal. These warnings do not crash functions but clutter CloudWatch logs and can obscure real errors. The root cause is often not user code but boto3's underlying botocore library, which historically used utcnow() internally for request signing. The fix involves replacing deprecated calls with datetime.datetime.now(datetime.UTC) and upgrading boto3, botocore, and AWS Lambda Powertools to their latest versions. Developers can also set the PYTHONWARNINGS=error::DeprecationWarning environment variable to generate full stack traces that pinpoint the exact source of remaining warnings.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in