Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

How do you return a JSON result from a python handler? #28

Closed
SingaporeClouds opened this issue Apr 12, 2017 · 8 comments
Closed

How do you return a JSON result from a python handler? #28

SingaporeClouds opened this issue Apr 12, 2017 · 8 comments

Comments

@SingaporeClouds
Copy link

In a node handler, you can return results with the passed in context.

exports.handler = function(event, context) {
  context.succeed({'Hello':'from handler'});
  return;
};

What is the equivalent to do this in Python so that I can evaluate the results coming back from a Python lambda call using dockerLambda? I can not call context.succeed() on the context passed to a python handler.

var lambdaCallbackResult = dockerLambda({
                dockerImage: "lambci/lambda:python2.7",
                event: {"some":"data"}});
console.log(lambdaCallbackResult);
@mhart
Copy link
Member

mhart commented Apr 12, 2017

The return value of the Python function should work: https://github.com/lambci/docker-lambda/blob/master/examples/docker-run-python2.7/lambda_function.py

@SingaporeClouds
Copy link
Author

SingaporeClouds commented Apr 12, 2017

I've tried using the python function return as implied from the example, but that does not work as expected.

# python_handler.py
import os
import sys

def lambda_handler(event, context):
    return { 
        "message" : "Returning from python handler"
    }

This will work when you call docker directly.
$ docker run -v "$PWD":/var/task lambci/lambda:python2.7 python_handler.lambda_handler
...
REPORT RequestId: ca86fb02-f856-4868-9384-995652163464 Duration: 8 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 14 MB
{"message": "Returning from python handler"}

// But if you call the same handler from node using docker-lambda, the result is undefined. 
// temp.js
var dockerLambda = require('docker-lambda');
var lambdaCallbackResult = dockerLambda({
                dockerImage: "lambci/lambda:python2.7",
                handler: "python_handler.lambda_handler"
            });
console.log("From python -->",lambdaCallbackResult);

$ node temp.js
From python --> undefined

I'm unclear how to get lambdaCallBackResult to contain the value returned from the python lambda_handler.

@mhart
Copy link
Member

mhart commented Apr 12, 2017

Hmmm, not sure why that's happening off the top of my head – quickly glancing at the code responsible seems to indicate it should work (it reads and parses from stdout – and I'm pretty sure that result is going to stdout)

Not in front of my comp at the moment, but should be able to look into this more later.

Here's what I'd be looking at:

https://github.com/lambci/docker-lambda/blob/master/python2.7/run/runtime-mock.py#L142
and
https://github.com/lambci/docker-lambda/blob/master/index.js#L61

@mhart
Copy link
Member

mhart commented Apr 12, 2017

(Unless it's just simply a case of needing to trim() the stdout? We swallow all parse errors, so something like that could be happening)

@SingaporeClouds
Copy link
Author

I see the same issue trying to retrieve the "It works!" string back from the https://github.com/lambci/docker-lambda/blob/master/examples/docker-run-python2.7/lambda_function.py example. These few lines of code replicate the same issue.

// temp.js
var dockerLambda = require('docker-lambda');
var lambdaCallbackResult = dockerLambda({
                dockerImage: "lambci/lambda:python2.7",
                handler: "lambda_function.lambda_handler"
            });
console.log("From python -->",lambdaCallbackResult);

$ node temp.js
From python --> undefined

@SingaporeClouds
Copy link
Author

And for completeness, I confirmed that the result and the result only is going to standard out when called with exec.

// dockerLambda will not return result.
var dockerLambda = require('docker-lambda');
var lambdaCallbackResult = dockerLambda({
                dockerImage: "lambci/lambda:python2.7",
                handler: "lambda_function.lambda_handler"
            });
console.log("From dockerLambda -->",lambdaCallbackResult);

// exec will return result on stdout.
var exec = require('child_process').exec;
var cmd = 'docker run -v "$PWD":/var/task lambci/lambda:python2.7 lambda_function.lambda_handler';
exec(cmd, function(error, stdout, stderr) {
  console.log("From exec -->",stdout);
});

$ node temp.js
From dockerLambda --> undefined
From exec --> "It works!"

@mhart
Copy link
Member

mhart commented Aug 13, 2017

@SingaporeClouds can you try with the latest version? I believe this might've been fixed in 92f59a6

@mhart
Copy link
Member

mhart commented May 6, 2019

Closing due to lack of activity

@mhart mhart closed this as completed May 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants