Reputation: 802
Im currently writing a python script that interacts with some AWS lambda functions. In one of the functions, my response contains a list which I need in my script.
Problem is that when I use the invoke()
function, the response is a json which contains request information.
response = aws_lambdaClient.invoke(FunctionName = 'functionName', Payload = payload)
The function that im using has this as a return
return {'names': aList, 'status': 'Success!'}
If I print out the response, I get this:
{'ResponseMetadata': {'RequestId': 'xxxxxxxxx', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Thu, 07 Nov 2019 14:28:25 GMT', 'content-type': 'application/json', 'content-length': '51', 'connection': 'keep-alive', 'x-amzn-requestid': 'xxxxxxxxxx', 'x-amzn-remapped-content-length': '0', 'x-amz-executed-version': '$LATEST', 'x-amzn-trace-id': 'root=xxxxxxxxx;sampled=0'}, 'RetryAttempts': 0}, 'StatusCode': 200, 'ExecutedVersion': '$LATEST', 'Payload': <botocore.response.StreamingBody object at 0x0000023D15716048>}
And id like to get
{'names': aList, 'status': 'Success!'}
Any idea on how can I achieve this? Or should I find another way of getting the data (Maybe putting the list i need in an s3 bucket and then getting it from there).
Upvotes: 5
Views: 15667
Reputation: 171
You must be using InvocationType='Event', You must use InvocationType='RequestResponse',
As per the document
RequestResponse (default) – Invoke the function synchronously. Keep the connection open until the function returns a response or times out. The API response includes the function response and additional data.
Event – Invoke the function asynchronously. Send events that fail multiple times to the function's dead-letter queue (if one is configured). The API response only includes a status code.
DryRun – Validate parameter values and verify that the user or role has permission to invoke the function.
Upvotes: 2
Reputation: 3394
Manuel,
as mentioned, the return info is inside the Payload element in the returned json. Payload is a boto3 object type that you need to access it's contents through it's read() method.
The code I used to get the python dictionary that I return from my lambda functions is this:
payload = json.loads(response['Payload'].read())
statusCode = payload.get('statusCode')
message = payload.get('message')
results = payload.get('results')
Upvotes: 19