You do not need to return any value, but some calling systems want a response.
For example, if the Lambda function is invoked by API Gateway, it needs to send a response back to the original caller. See: Handle Lambda errors in API Gateway
It can also be used to pass back details errors: AWS Lambda function errors in Python
Answer from John Rotenstein on Stack OverflowYou do not need to return any value, but some calling systems want a response.
For example, if the Lambda function is invoked by API Gateway, it needs to send a response back to the original caller. See: Handle Lambda errors in API Gateway
It can also be used to pass back details errors: AWS Lambda function errors in Python
https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
Example:
import os
import json
def lambda_handler(event, context):
json_region = os.environ['AWS_REGION']
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Region ": json_region
})
}
return value of python AWS Lambda.
return value from lambda function
Returning a result from a lambda function
amazon web services - how to get return response from AWS Lambda function - Stack Overflow
I'm working on a c# application and using AWS lambda functions in the backend. The lambda function is working correctly and i'm able to call it from the application. The part I'm having trouble with is getting the code to wait for the result from the lambda function to be returned before continuing. I've looked into using the async/await pattern but i'm getting compile errors because AmazonLambda.InvokeAsync returns null.
This is the code what is correctly invoking the function and prints out the response but I'd like to instead return the response to the calling method. I've also tried changing the return from void to string and adding a return to the callback function but I get this error: "Anonymous function converted to a void returning delegate cannot return a value"
Any help is appreciated.
public void Invoke()
{
InvokeRequest invokeRequest = new InvokeRequest()
{
FunctionName = FunctionName,
Payload = Payload
};
Client.InvokeAsync(invokeRequest, responseObject =>
{
if (responseObject.Exception == null)
{
Debug.Log("LAMBDA SUCCESS: " + Encoding.ASCII.GetString(responseObject.Response.Payload.ToArray()));
}
else
{
Debug.Log("LAMBDA ERR: " + responseObject.Exception);
}
});
}
A 202 response means Accepted. It is a successful response but is telling you that the action you have requested has been initiated but has not yet completed. The reason you are getting a 202 is because you invoked the Lambda function asynchronously. Your InvocationType parameter is set to Event. If you want to make a synchronous call, change this to RequestResponse.
Once you do that, you can get the returned data like this:
data = invoke_response['Payload'].read()
try: data = invoke_response['Payload'].read()
read() because it is a StreamingBody object
<botocore.response.StreamingBody object at 0x110b91c50>
It is in the boto3 docs. You can find more details about this here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html#actions
Good Evening,
I'm relatively new to writing AWS Lambdas, and I've been writing some in Python lately.
My lambdas are generally structured where the handler function instantiating another class which calls a method to actually do the logic of the Lambda, which seems typical. Once this logic is done, it returns to the handler function and then exits. For perspective, these are lambdas that are invoked by an S3 event, do some file processing, insert data into a DB, and then exit.
I'm wondering if there are guidelines/tips/best practices on what the handler function should return, depending on whether there was a success or not. Additionally, having actual return code/exit codes would make testing easier, I imagine. I've seen some people use like HTTP Status codes returned as a dictionary, but I'm not sure if there's any benefit to that.
TLDR: Is there a standard or best practice for return/exit codes from AWS Lambdas - Success or Failures?