Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

"DeprecationWarning: datetime.datetime.utcnow() is deprecated" — fixing the Python 3.12 warning in AWS Lambda

The moment you bump a Lambda function to the python3.12 runtime, CloudWatch Logs often starts showing warnings like this — once per cold start, or during your test suite against a Python 3.12 environment: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC:…

When you upgrade your Lambda function to the Python 3.12 runtime, CloudWatch Logs begin displaying warnings each time the function is cold started or during testing against a Python 3.12 environment. The warning reads, "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version."

The issue arises because Python 3.12 deprecates the utcnow() method, which returns a naive datetime object without timezone information. Most modern libraries return aware datetimes with tzinfo set to UTC, causing a TypeError when comparing naive and aware datetimes.

To resolve this warning, replace each occurrence of datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc) or datetime.datetime.now(datetime.UTC). For timestamp conversions, use datetime.datetime.fromtimestamp(ts, datetime.timezone.utc) (Python 3.11+) or datetime.datetime.fromtimestamp(ts, datetime.UTC) (Python 3.9+). Python 3.11 introduced the datetime.UTC shorthand, added in Python 3.11.

To identify all instances of utcnow() and utcfromtimestamp() in your deployment package, run `grep -r "utcnow()" *.py --include = *.py` from the root of your Lambda project. This will surface every call site, both in your code and in any vendored dependencies.

If the warning originates from boto3's request-signing code, upgrade to the latest versions of boto3 and botocore to patch the internal calls to datetime.utcnow(). If you use the AWS Lambda Powertools library, it has already been updated to include correct utcnow() calls.

To pinpoint the exact location generating the warning, set the environment variable PYTHONWARNINGS to "error::DeprecationWarning". This turns all DeprecationWarnings into exceptions, providing a full traceback that points to the specific file and line.

Once you've resolved the issue, confirm it's gone by running pytest with warnings promoted to errors: `python -W error::DeprecationWarning -m pytest`. A clean run indicates no remaining occurrences of utcnow() or utcfromtimestamp() in your code or dependencies. If new warnings are found from external dependencies, report them to the upstream project or vendor a patched version.

Python 3.12 is the target runtime for Lambda runtimes python3.8, python3.9, and python3.10, which are all being deprecated and will be phased out by Q1 2027. Fixing the utcnow() warning is a straightforward one-liner per call site, leaving your logs free from this noise during actual troubleshooting. You can perform an end-of-life (EOL) scan of your entire AWS account at eolkits.com/scan to identify deprecated runtimes, AMIs, and OS versions in under a minute.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Oracle 26ai- Installing Oracle Grid Infrastructure for a Standalone Server Using the Command-Line Interface

Before Oracle AI Database 26ai, Oracle Grid Infrastructure could be installed using a response file. For example( using a response file ): ./gridSetup.sh -silent \ INVENTORY_LOCATION = /oraInventory \ SELECTED_LANGUAGES = en \ ORACLE_BASE = /grid/base \ oracle.install.option = HA_CONFIG \ oracle.install.asm.OSDBA = asmdba \…

  • Oracle Grid Infrastructure installable via command-line interface in Oracle AI Database 26ai
  • -configureStandaloneServer option enables standalone server installation
  • Passwords for SYS, ASMSNMP, root required during installation

More from Monday 17 August →