title: How to invoke an AWS Lambda function from EC2 instances with python asyncio
body: <p>I recently posted a question about <a href="https://stackoverflow.com/questions/43257091/how-to-allow-invoking-an-aws-lambda-function-only-from-ec2-instances-inside-a-vp">How to allow invoking an AWS Lambda function only from EC2 instances inside a VPC</a>.
  I managed to get it working by attaching an IAM role with an "AWS lambda role" policy to the EC2 instances and now I can invoke the lambda function using boto3.</p>
  
  <p>Now, I would like to make the call to the lambda function asynchronously using the asyncio await syntax. I read that the lambda function offers an asynchronous version by setting <code>InvokeType='Event'</code>, but that actually makes the call return immediately without getting the result of the function.</p>
  
  <p>Since the function takes some time and I would like to launch many in parallel I would like to avoid blocking the execution while waiting for the function to return.</p>
  
  <p>I tried using aiobotocore but that is only supporting basic 's3' service functionalities.</p>
  
  <p>The best way to solve this (in humble opinion) would be to use the AWS API Gateway service to invoke the lambda function through a GET/POST request that can be easily handled using aiohttp.</p>
  
  <p>Nevertheless I don't manage to make it work.</p>
  
  <p>I added to the EC2 IAM role the policy "AmazonAPIGatewayInvokeFullAccess" but every time I try to:</p>
  
  <pre><code>import requests
  r = requests.get('https://url_to_api_gateway_for_function')
  </code></pre>
  
  <p>I get a forbidden response <code>&lt;Response [403]&gt;</code>.</p>
  
  <p>I created the API Gateway using directly the trigger in the lambda function.</p>
  
  <p>I also tried to edit the API Gateway settings, by adding a post method to the function path and setting the "AWS_IAM" authentication and then deploying it as "prod" deployment...no luck. Still same forbidden response. When I test it through the "test screen on the API gateway, it works fine".</p>
  
  <p>Any idea how to fix this? Am I missing some step?</p>