Parse AI

How to Use Async Python Functions in AWS Lambda in 2026

And why you should be using async handlers to improve your code.

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 function

async def main(event: dict, context=None):
    # Insert business logic here: query DB, call endpoints, etc
    return "Hello World"

2. Use asyncio to run your main function in the existing event loop

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 🎉

Why should I use async functions?

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.

Powered by Synscribe

1/1/2026
Related Posts
Every Web Scraper Must Use These 4 Techniques

Every Web Scraper Must Use These 4 Techniques

Four Python techniques that will vastly improve your web scrapers.

Read Full Story
How to Scrape Amazon Prices (all code included)

How to Scrape Amazon Prices (all code included)

Web scraping guide for beginners to collect prices from products on amazon.com

Read Full Story
© Parse AI 2026