{
  "id": 1443880,
  "title": "\"DeprecationWarning: datetime.datetime.utcnow() is deprecated\" — fixing the Python 3.12 warning in AWS Lambda",
  "url": "https://urgent.news/2026/08/17/deprecationwarning-datetime-datetime-utcnow-is-deprecated-fixing-the",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-17T07:17:03.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/ntoledo319/deprecationwarning-datetimedatetimeutcnow-is-deprecated-fixing-the-python-312-warning-in-5ggc"
  },
  "original_language": "en",
  "account": "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.\"\n\nThe 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.\n\nTo 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.\n\nTo 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.\n\nIf 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.\n\nTo 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.\n\nOnce 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.\n\nPython 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.",
  "summary": "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:…",
  "key_points": [
    "DeprecationWarning issued for datetime.datetime.utcnow() in Python 3.12",
    "Upgrade boto3 and botocore, use AWS Lambda Powertools to resolve warning"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}