Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The better way to execute Go on Amazon Lambda (see: http://blog.0x82.com/2014/11/24/aws-lambda-functions-in-go/)
var child_process = require('child_process');
exports.handler = function(event, context) {
var proc = spawn('./test', [ JSON.stringify(event) ], { stdio: 'inherit' });
proc.on('close', function(code){
if(code !== 0) {
return context.done(new Error("Process exited with non-zero status code"));
}
context.done(null);
});
}
@ThisIsMissEm

This comment has been minimized.

Copy link
Owner Author

@ThisIsMissEm ThisIsMissEm commented Nov 25, 2014

The biggest issue in the original was the string concatenation in exec rather than using execFile with an arguments array. See here for more on this vulnerability: https://blog.liftsecurity.io/2014/08/19/Avoid-Command-Injection-Node.js

However, here, I'm not using execFile either, as the expected behaviour is to have the ./test script output to the handler.js's standard output streams. That is, using spawn with stdio: 'inherit' means that we direct the stdout / stderr / stdin of ./test to be that of handler.js. I'm also correctly triggering errors for non-zero exit codes by using the context.done(err, msg) syntax.

@rubenfonseca

This comment has been minimized.

Copy link

@rubenfonseca rubenfonseca commented Nov 25, 2014

Thanks @miksago! It's clear that I'm really not the best node developer :-) I've updated my blog with your suggestion!

@escholtz

This comment has been minimized.

Copy link

@escholtz escholtz commented Jul 22, 2015

Is child_process.spawn required on line 4?

@arihantagarwal

This comment has been minimized.

Copy link

@arihantagarwal arihantagarwal commented Sep 7, 2015

@escholtz: Yes.

@tmaiaroto

This comment has been minimized.

Copy link

@tmaiaroto tmaiaroto commented Oct 11, 2015

This has been really helpful for me in learning how to do this, thanks for sharing. I also stumbled upon an even better way: https://github.com/jasonmoo/lambda_proc

@yvele

This comment has been minimized.

Copy link

@yvele yvele commented Oct 25, 2016

You can now run vanilla Go on AWS Lambda, in a fast (<5ms) and clean way (log/panic)
https://github.com/eawsy/aws-lambda-go @eawsy
This feels like native Go support.. with a single file 😍

PS: No Node.js wrapper (using Python) and cgo under the hood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment