How to asynchronously call aws lambda function using Python requests

Viewed 830

I know how to invoke AWS lambda function asynchronously using boto3 in python.

Here's the code I'm using:


import boto3
import json

lambda_client = boto3.client('lambda',
                             region_name='ap-northeast-2',
                             aws_access_key_id='XXXXXXXXXX',
                             aws_secret_access_key='XXXXXXXXXXX')
data = {"data1":"mydata"}

response = lambda_client.invoke(FunctionName="my_lambda_function",
                                InvocationType="Event",         ###for asynchronous purposes
                                Payload=json.dumps(data)
                                )

but I want to invoke lambda asynchronously using requests instead of invoke method

ex):

import requests
param= data = {"data1":"mydata"}
url = "https://xxxxx.execute-api.ap-northeast-2.amazonaws.com/my_lambda_function)"
res = requests.get(url, params=param)

Should I implement the code for asynchronous calls myself? Is there a simple asynchronous way to call lambda requests?

1 Answers

You can't have access to a Lambda function without AWS credentials. Even if you implement your own HTTP request (reinventing the wheel), you will need to sign your request with valid credentials and permissions to invoke the Lambda function.

What you need is an API Gateway integration with your Lambda. It will expose an HTTP API, which you can call with the requests library.

If you want the Lambda invocation by API Gateway to be asynchronous, follow these instructions: Set up asynchronous invocation of the backend Lambda function

Related