Created
          October 8, 2015 00:44 
        
      - 
      
- 
        Save rayh/1ee0f6ce54cc9fafbb06 to your computer and use it in GitHub Desktop. 
    Spawn binary from AWS Lambda
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var fs = require('fs'); | |
| var Q = require('q'); | |
| var spawn = require('child_process').spawn; | |
| var path = require('path'); | |
| // Make sure you append the path with the lambda deployment path, so you can | |
| // execute your own binaries in, say, bin/ | |
| process.env["PATH"] = process.env["PATH"] + ":" + process.env["LAMBDA_TASK_ROOT"] + '/bin/'; | |
| function spawnCmd(cmd, args, opts) { | |
| var opts = opts||{}; | |
| var basename = path.basename(cmd); | |
| console.log("[spawn]", cmd, args.join(' '), opts); | |
| var deferred = Q.defer(); | |
| child = spawn(cmd, args, opts); | |
| child.stdout.on('data', function(chunk) { | |
| console.log("[" + basename + ":stdout] " + chunk); | |
| }); | |
| child.stderr.on('data', function(chunk) { | |
| console.log("[" + basename + ":stderr] " + chunk); | |
| }); | |
| child.on('error', function (error) { | |
| console.log("[" + basename + "] unhandled error:",error); | |
| deferred.reject(new Error(error)); | |
| }); | |
| child.on('close', function (code, signal) { | |
| if(signal) { | |
| deferred.reject("Process killed with signal " + signal); | |
| } else if(code==0) { | |
| deferred.resolve(code); | |
| } else { | |
| deferred.reject("Process exited with code " + code); | |
| } | |
| console.log("[" + basename + "] child process exited with code",code, "signal", signal); | |
| }); | |
| return deferred.promise; | |
| } | |
| exports.handler = function(event, context) { | |
| spawnCmd("/usr/bin/uname", ["-a"], { | |
| cwd: '/tmp' | |
| }).then(function(result) { | |
| context.succeed(result); | |
| }, function(err) { | |
| context.fail(err); | |
| }); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Thanks for this, it was really helpful to me!
I ended up changing it a bit to use
child-process-promise(and notq):I also removed the "basename" part because it didn't happen to be necessary for my usecase.
Hope this is helpful to any Node newbs like me who find this through google search :)