I have a small computer simulation set up in AWS Lambda (Python 3.8) which requires to run a complex function several hundred times with different input parameters. The calculations don't depend on one another, so they can run in parallel. I have a "parent function" to provide the inputs and a "child function" that will be called multiple times to do the calculations. The parent function then receives the results and I can move forward to working with them.
The issue I am facing is that once I switch from consecutive invocation (InvocationType = 'RequestResponse') to concurrent invocation (InvocationType = 'Event'), I get an error of the type [JSONDecodeError: Expecting value: line 1 column 1 (char 0)]. Even with a high resource Lambda "child function" my calculation takes 10-20 seconds per run, so I need these to run in parralel. All the responses to similar questions and programming examples I found so far refer to examples that don't collect the results in the parent function. Does anyone know how to adjust my code to allow for fully parralel computation and direct use of the results in the parent function (storing them as arrays)? As an example, I have a code sample that I copied from an online tutorial. It works perfectly fine until I switch to parallel (InvocationType = 'Event') invocation.
Parent function (this is where I change the InvocationType from 'RequestResponse' to 'Event'):
import json
import boto3
client = boto3.client('lambda')
def lambda_handler(event,context):
# Creation of simple inputs for the child function
for i in range(0,10):
quantity = i
price = i
inputParams = {
"Quantity" : quantity,
"UnitPrice" : price
}
response = client.invoke(
FunctionName = 'arn:aws:lambda:REGION-and-ID:function:ChildFunction',
InvocationType = 'RequestResponse',
Payload = json.dumps(inputParams)
)
responseFromChild = json.load(response['Payload'])
# Simply printing the output without processing it
print('\n')
print(responseFromChild['Amount'])
Child function (simplified, processes my repeating calculation):
import json
def lambda_handler(event, context):
# Read the input parameters
quantity = event['Quantity']
unitPrice = event['UnitPrice']
# Define output array and perform calculation
amount = []
for i in range (0, 3):
amount.append(quantity * unitPrice * i)
# Format and return the result
return {
'Amount' : amount
}