This rule raises an issue when the AWS Lambda handler function is declared with async.

Why is this an issue?

The standard AWS Lambda Python runtime is designed to invoke a synchronous handler function. While Python’s asyncio library can be used inside a synchronous handler, by calling asyncio.run(), the handler function itself cannot be declared with async def. Doing so will lead to a runtime error.

How to fix it

To fix this issue, remove the async keyword from the handler function definition. The asynchronous part of the code can be moved to its own async function.

Code examples

Noncompliant code example

def some_logic():
  ...

async def lambda_handler(event, context): # Noncompliant: the handler is defined with async
  result = some_logic()
  return {"status": result}

Compliant solution

import asyncio

async def some_logic():
  ...

def lambda_handler(event, context):
  result = asyncio.run(some_logic())
  return {"status": result}

Resources

Documentation