title: Can you have an async handler in Lambda Python 3.6?
body: <p>I've made Lambda functions before but not in Python. I know in Javascript Lambda supports the handler function being asynchronous, but I get an error if I try it in Python.</p>
  <p>Here is the code I am trying to test:</p>
  <pre class="lang-py prettyprint-override"><code>async def handler(event, context):
      print(str(event))
      return { 
          'message' : 'OK'
      }
  </code></pre>
  <p>And this is the error I get:</p>
  <pre><code>An error occurred during JSON serialization of response: &lt;coroutine object handler at 0x7f63a2d20308&gt; is not JSON serializable
  Traceback (most recent call last):
    File &quot;/var/lang/lib/python3.6/json/__init__.py&quot;, line 238, in dumps
      **kw).encode(obj)
    File &quot;/var/lang/lib/python3.6/json/encoder.py&quot;, line 199, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File &quot;/var/lang/lib/python3.6/json/encoder.py&quot;, line 257, in iterencode
      return _iterencode(o, 0)
    File &quot;/var/runtime/awslambda/bootstrap.py&quot;, line 149, in decimal_serializer
      raise TypeError(repr(o) + &quot; is not JSON serializable&quot;)
  TypeError: &lt;coroutine object handler at 0x7f63a2d20308&gt; is not JSON serializable
  
  /var/runtime/awslambda/bootstrap.py:312: RuntimeWarning: coroutine 'handler' was never awaited
    errortype, result, fatal = report_fault(invokeid, e)
  </code></pre>
  <hr />
  <p><strong>EDIT 2021</strong>:</p>
  <p>Since this question seems to be gaining traction, I assume people are coming here trying to figure out how to get <code>async</code> to work with AWS Lambda as I was. The bad news is that even now more than a year later, there still isn't any support by AWS to have an asynchronous handler in a Python-based Lambda function. (I have no idea why, as NodeJS-based Lambda functions can handle it perfectly fine.)</p>
  <p>The good news is that since Python 3.7, there is a simple workaround in the form of <code>asyncio.run</code>:</p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  
  def lambda_handler(event, context):
      # Use asyncio.run to synchronously &quot;await&quot; an async function
      result = asyncio.run(async_handler(event, context))
      
      return {
          'statusCode': 200,
          'body': result
      }
  
  async def async_handler(event, context):
      # Put your asynchronous code here
      await asyncio.sleep(1)
      
      return 'Success'
  </code></pre>
  <p><em>Note: The selected answer says that using <code>asyncio.run</code> is not the proper way of starting an asynchronous task in Lambda. In general, they are correct because if some other resource in your Lambda code creates an event loop (a database/HTTP client, etc.), it's wasteful to create another loop and it's better to operate on the existing loop using <code>asyncio.get_event_loop</code>.</em></p>
  <p><em>However, if an event loop does not yet exist when your code begins running, <code>asyncio.run</code> becomes the only (simple) course of action.</em></p>