2017-09-29 7 views
0

私は、Rubyスクリプトを実行するLambda関数を構築する方法を説明するAWS tutorialを続けました。私の唯一の混乱は、ラムダ関数の結果としてRubyスクリプトから結果を返す方法です。Rubyスクリプトを実行するLambda関数の結果を返します

const exec = require('child_process').exec; 

exports.handler = function(event, context) { 
    const child = exec('./lambdaRuby.rb ' + ''' + JSON.stringify(event) + ''', (result) => { 
     // Resolve with result of process 
     context.done(result); 
    }); 

    // Log process stdout and stderr 
    child.stdout.on('data', console.log); 
    child.stderr.on('data', console.error); 
} 
+0

...のように記述すべきですか? – dashmug

+0

LambdaはネイティブでRubyをサポートしていないので、ノード –

+0

に移植されていないRuby gemを使用しています。コールバックであるハンドラ関数に別のパラメータを追加すると思います。http://docs.awsの例を参照してください。 .amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html –

答えて

1

参照したチュートリアルは、ラムダの旧バージョンのノードに基づいています。

あなたはノード6.10を使用している場合、それはあなたがちょうどあなたのすべての面倒を保存するNodeJSにRubyでのロジックを書き換えていないのはなぜ

const { exec } = require('child_process'); 

exports.handler = function(event, context, callback) { 
    const child = exec(`./lambdaRuby.rb '${JSON.stringify(event)}'`, (error, stdout) => { 
     if (error) return callback(error); 

     return callback(null, stdout) 
    }); 

    // Log process stdout and stderr 
    child.stdout.on('data', console.log); 
    child.stderr.on('data', console.error); 
} 
+0

この回答は、スクリプトの出力を返すためのものです。 APIゲートウェイでこれを使用したい場合でも、依然としてレスポンスオブジェクトの構造を変更する必要があります。 – dashmug

関連する問題