AWS Lambda does not support async Python functions as the handler function out of the box. But, there’s a pretty easy workaround that allows all the same functionality of normal async Python code.
1. Write your main business logic in an async functionasync def main(event: dict, context=None):
# Insert business logic here: query DB, call endpoints, etc
return "Hello World"AWS Lambda attempts to reuse execution context across multiple invocations. In this case, it means that when running several executions at once, an event loop will already be open. Using a simple asyncio.run() will cause downstream errors because it closes the event loop and opens a new one.
To avoid that, we need to access the existing event loop:
import asyncio
async def main(event: dict, context=None):
# Insert business logic here: query DB, call endpoints, etc
return "Hello World"
def lambda_handler(event: dict, context=None):
loop = asyncio.get_event_loop()
return loop.run_until_complete(main(event, context))Now, just make sure the Lambda config points to lambda_handler() and you’ll be able to use async functions throughout your code 🎉
As I have been writing an exorbitant amount of Python code in the last few years after starting my data collection company, I find myself leaning towards async def main() far more than def main() .
I spend a lot of time web scraping, which often requires sending thousands of network requests to scrape a single website. Async code makes everything run much quicker and allows for a lot more flexibility.
Having built several FastAPI apps as well, I almost exclusively use async functions. Concurrent database queries and third party API calls are that much easier to work with.
I wrote a full article about implementing async Python here.

Four Python techniques that will vastly improve your web scrapers.
Read Full Story
Web scraping guide for beginners to collect prices from products on amazon.com
Read Full Story